diff --git a/LICENSE b/LICENSE
new file mode 100644
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,373 @@
+Mozilla Public License Version 2.0
+==================================
+
+1. Definitions
+--------------
+
+1.1. "Contributor"
+    means each individual or legal entity that creates, contributes to
+    the creation of, or owns Covered Software.
+
+1.2. "Contributor Version"
+    means the combination of the Contributions of others (if any) used
+    by a Contributor and that particular Contributor's Contribution.
+
+1.3. "Contribution"
+    means Covered Software of a particular Contributor.
+
+1.4. "Covered Software"
+    means Source Code Form to which the initial Contributor has attached
+    the notice in Exhibit A, the Executable Form of such Source Code
+    Form, and Modifications of such Source Code Form, in each case
+    including portions thereof.
+
+1.5. "Incompatible With Secondary Licenses"
+    means
+
+    (a) that the initial Contributor has attached the notice described
+        in Exhibit B to the Covered Software; or
+
+    (b) that the Covered Software was made available under the terms of
+        version 1.1 or earlier of the License, but not also under the
+        terms of a Secondary License.
+
+1.6. "Executable Form"
+    means any form of the work other than Source Code Form.
+
+1.7. "Larger Work"
+    means a work that combines Covered Software with other material, in
+    a separate file or files, that is not Covered Software.
+
+1.8. "License"
+    means this document.
+
+1.9. "Licensable"
+    means having the right to grant, to the maximum extent possible,
+    whether at the time of the initial grant or subsequently, any and
+    all of the rights conveyed by this License.
+
+1.10. "Modifications"
+    means any of the following:
+
+    (a) any file in Source Code Form that results from an addition to,
+        deletion from, or modification of the contents of Covered
+        Software; or
+
+    (b) any new file in Source Code Form that contains any Covered
+        Software.
+
+1.11. "Patent Claims" of a Contributor
+    means any patent claim(s), including without limitation, method,
+    process, and apparatus claims, in any patent Licensable by such
+    Contributor that would be infringed, but for the grant of the
+    License, by the making, using, selling, offering for sale, having
+    made, import, or transfer of either its Contributions or its
+    Contributor Version.
+
+1.12. "Secondary License"
+    means either the GNU General Public License, Version 2.0, the GNU
+    Lesser General Public License, Version 2.1, the GNU Affero General
+    Public License, Version 3.0, or any later versions of those
+    licenses.
+
+1.13. "Source Code Form"
+    means the form of the work preferred for making modifications.
+
+1.14. "You" (or "Your")
+    means an individual or a legal entity exercising rights under this
+    License. For legal entities, "You" includes any entity that
+    controls, is controlled by, or is under common control with You. For
+    purposes of this definition, "control" means (a) the power, direct
+    or indirect, to cause the direction or management of such entity,
+    whether by contract or otherwise, or (b) ownership of more than
+    fifty percent (50%) of the outstanding shares or beneficial
+    ownership of such entity.
+
+2. License Grants and Conditions
+--------------------------------
+
+2.1. Grants
+
+Each Contributor hereby grants You a world-wide, royalty-free,
+non-exclusive license:
+
+(a) under intellectual property rights (other than patent or trademark)
+    Licensable by such Contributor to use, reproduce, make available,
+    modify, display, perform, distribute, and otherwise exploit its
+    Contributions, either on an unmodified basis, with Modifications, or
+    as part of a Larger Work; and
+
+(b) under Patent Claims of such Contributor to make, use, sell, offer
+    for sale, have made, import, and otherwise transfer either its
+    Contributions or its Contributor Version.
+
+2.2. Effective Date
+
+The licenses granted in Section 2.1 with respect to any Contribution
+become effective for each Contribution on the date the Contributor first
+distributes such Contribution.
+
+2.3. Limitations on Grant Scope
+
+The licenses granted in this Section 2 are the only rights granted under
+this License. No additional rights or licenses will be implied from the
+distribution or licensing of Covered Software under this License.
+Notwithstanding Section 2.1(b) above, no patent license is granted by a
+Contributor:
+
+(a) for any code that a Contributor has removed from Covered Software;
+    or
+
+(b) for infringements caused by: (i) Your and any other third party's
+    modifications of Covered Software, or (ii) the combination of its
+    Contributions with other software (except as part of its Contributor
+    Version); or
+
+(c) under Patent Claims infringed by Covered Software in the absence of
+    its Contributions.
+
+This License does not grant any rights in the trademarks, service marks,
+or logos of any Contributor (except as may be necessary to comply with
+the notice requirements in Section 3.4).
+
+2.4. Subsequent Licenses
+
+No Contributor makes additional grants as a result of Your choice to
+distribute the Covered Software under a subsequent version of this
+License (see Section 10.2) or under the terms of a Secondary License (if
+permitted under the terms of Section 3.3).
+
+2.5. Representation
+
+Each Contributor represents that the Contributor believes its
+Contributions are its original creation(s) or it has sufficient rights
+to grant the rights to its Contributions conveyed by this License.
+
+2.6. Fair Use
+
+This License is not intended to limit any rights You have under
+applicable copyright doctrines of fair use, fair dealing, or other
+equivalents.
+
+2.7. Conditions
+
+Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
+in Section 2.1.
+
+3. Responsibilities
+-------------------
+
+3.1. Distribution of Source Form
+
+All distribution of Covered Software in Source Code Form, including any
+Modifications that You create or to which You contribute, must be under
+the terms of this License. You must inform recipients that the Source
+Code Form of the Covered Software is governed by the terms of this
+License, and how they can obtain a copy of this License. You may not
+attempt to alter or restrict the recipients' rights in the Source Code
+Form.
+
+3.2. Distribution of Executable Form
+
+If You distribute Covered Software in Executable Form then:
+
+(a) such Covered Software must also be made available in Source Code
+    Form, as described in Section 3.1, and You must inform recipients of
+    the Executable Form how they can obtain a copy of such Source Code
+    Form by reasonable means in a timely manner, at a charge no more
+    than the cost of distribution to the recipient; and
+
+(b) You may distribute such Executable Form under the terms of this
+    License, or sublicense it under different terms, provided that the
+    license for the Executable Form does not attempt to limit or alter
+    the recipients' rights in the Source Code Form under this License.
+
+3.3. Distribution of a Larger Work
+
+You may create and distribute a Larger Work under terms of Your choice,
+provided that You also comply with the requirements of this License for
+the Covered Software. If the Larger Work is a combination of Covered
+Software with a work governed by one or more Secondary Licenses, and the
+Covered Software is not Incompatible With Secondary Licenses, this
+License permits You to additionally distribute such Covered Software
+under the terms of such Secondary License(s), so that the recipient of
+the Larger Work may, at their option, further distribute the Covered
+Software under the terms of either this License or such Secondary
+License(s).
+
+3.4. Notices
+
+You may not remove or alter the substance of any license notices
+(including copyright notices, patent notices, disclaimers of warranty,
+or limitations of liability) contained within the Source Code Form of
+the Covered Software, except that You may alter any license notices to
+the extent required to remedy known factual inaccuracies.
+
+3.5. Application of Additional Terms
+
+You may choose to offer, and to charge a fee for, warranty, support,
+indemnity or liability obligations to one or more recipients of Covered
+Software. However, You may do so only on Your own behalf, and not on
+behalf of any Contributor. You must make it absolutely clear that any
+such warranty, support, indemnity, or liability obligation is offered by
+You alone, and You hereby agree to indemnify every Contributor for any
+liability incurred by such Contributor as a result of warranty, support,
+indemnity or liability terms You offer. You may include additional
+disclaimers of warranty and limitations of liability specific to any
+jurisdiction.
+
+4. Inability to Comply Due to Statute or Regulation
+---------------------------------------------------
+
+If it is impossible for You to comply with any of the terms of this
+License with respect to some or all of the Covered Software due to
+statute, judicial order, or regulation then You must: (a) comply with
+the terms of this License to the maximum extent possible; and (b)
+describe the limitations and the code they affect. Such description must
+be placed in a text file included with all distributions of the Covered
+Software under this License. Except to the extent prohibited by statute
+or regulation, such description must be sufficiently detailed for a
+recipient of ordinary skill to be able to understand it.
+
+5. Termination
+--------------
+
+5.1. The rights granted under this License will terminate automatically
+if You fail to comply with any of its terms. However, if You become
+compliant, then the rights granted under this License from a particular
+Contributor are reinstated (a) provisionally, unless and until such
+Contributor explicitly and finally terminates Your grants, and (b) on an
+ongoing basis, if such Contributor fails to notify You of the
+non-compliance by some reasonable means prior to 60 days after You have
+come back into compliance. Moreover, Your grants from a particular
+Contributor are reinstated on an ongoing basis if such Contributor
+notifies You of the non-compliance by some reasonable means, this is the
+first time You have received notice of non-compliance with this License
+from such Contributor, and You become compliant prior to 30 days after
+Your receipt of the notice.
+
+5.2. If You initiate litigation against any entity by asserting a patent
+infringement claim (excluding declaratory judgment actions,
+counter-claims, and cross-claims) alleging that a Contributor Version
+directly or indirectly infringes any patent, then the rights granted to
+You by any and all Contributors for the Covered Software under Section
+2.1 of this License shall terminate.
+
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all
+end user license agreements (excluding distributors and resellers) which
+have been validly granted by You or Your distributors under this License
+prior to termination shall survive termination.
+
+************************************************************************
+*                                                                      *
+*  6. Disclaimer of Warranty                                           *
+*  -------------------------                                           *
+*                                                                      *
+*  Covered Software is provided under this License on an "as is"       *
+*  basis, without warranty of any kind, either expressed, implied, or  *
+*  statutory, including, without limitation, warranties that the       *
+*  Covered Software is free of defects, merchantable, fit for a        *
+*  particular purpose or non-infringing. The entire risk as to the     *
+*  quality and performance of the Covered Software is with You.        *
+*  Should any Covered Software prove defective in any respect, You     *
+*  (not any Contributor) assume the cost of any necessary servicing,   *
+*  repair, or correction. This disclaimer of warranty constitutes an   *
+*  essential part of this License. No use of any Covered Software is   *
+*  authorized under this License except under this disclaimer.         *
+*                                                                      *
+************************************************************************
+
+************************************************************************
+*                                                                      *
+*  7. Limitation of Liability                                          *
+*  --------------------------                                          *
+*                                                                      *
+*  Under no circumstances and under no legal theory, whether tort      *
+*  (including negligence), contract, or otherwise, shall any           *
+*  Contributor, or anyone who distributes Covered Software as          *
+*  permitted above, be liable to You for any direct, indirect,         *
+*  special, incidental, or consequential damages of any character      *
+*  including, without limitation, damages for lost profits, loss of    *
+*  goodwill, work stoppage, computer failure or malfunction, or any    *
+*  and all other commercial damages or losses, even if such party      *
+*  shall have been informed of the possibility of such damages. This   *
+*  limitation of liability shall not apply to liability for death or   *
+*  personal injury resulting from such party's negligence to the       *
+*  extent applicable law prohibits such limitation. Some               *
+*  jurisdictions do not allow the exclusion or limitation of           *
+*  incidental or consequential damages, so this exclusion and          *
+*  limitation may not apply to You.                                    *
+*                                                                      *
+************************************************************************
+
+8. Litigation
+-------------
+
+Any litigation relating to this License may be brought only in the
+courts of a jurisdiction where the defendant maintains its principal
+place of business and such litigation shall be governed by laws of that
+jurisdiction, without reference to its conflict-of-law provisions.
+Nothing in this Section shall prevent a party's ability to bring
+cross-claims or counter-claims.
+
+9. Miscellaneous
+----------------
+
+This License represents the complete agreement concerning the subject
+matter hereof. If any provision of this License is held to be
+unenforceable, such provision shall be reformed only to the extent
+necessary to make it enforceable. Any law or regulation which provides
+that the language of a contract shall be construed against the drafter
+shall not be used to construe this License against a Contributor.
+
+10. Versions of the License
+---------------------------
+
+10.1. New Versions
+
+Mozilla Foundation is the license steward. Except as provided in Section
+10.3, no one other than the license steward has the right to modify or
+publish new versions of this License. Each version will be given a
+distinguishing version number.
+
+10.2. Effect of New Versions
+
+You may distribute the Covered Software under the terms of the version
+of the License under which You originally received the Covered Software,
+or under the terms of any subsequent version published by the license
+steward.
+
+10.3. Modified Versions
+
+If you create software not governed by this License, and you want to
+create a new license for such software, you may create and use a
+modified version of this License if you rename the license and remove
+any references to the name of the license steward (except to note that
+such modified license differs from this License).
+
+10.4. Distributing Source Code Form that is Incompatible With Secondary
+Licenses
+
+If You choose to distribute Source Code Form that is Incompatible With
+Secondary Licenses under the terms of this version of the License, the
+notice described in Exhibit B of this License must be attached.
+
+Exhibit A - Source Code Form License Notice
+-------------------------------------------
+
+  This Source Code Form is subject to the terms of the Mozilla Public
+  License, v. 2.0. If a copy of the MPL was not distributed with this
+  file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+If it is not possible or desirable to put the notice in a particular
+file, then You may include the notice in a location (such as a LICENSE
+file in a relevant directory) where a recipient would be likely to look
+for such a notice.
+
+You may add additional accurate notices of copyright ownership.
+
+Exhibit B - "Incompatible With Secondary Licenses" Notice
+---------------------------------------------------------
+
+  This Source Code Form is "Incompatible With Secondary Licenses", as
+  defined by the Mozilla Public License, v. 2.0.
diff --git a/README.md b/README.md
new file mode 100644
--- /dev/null
+++ b/README.md
@@ -0,0 +1,69 @@
+# Statgrab
+
+Haskell bindings to the portable system statistics `libstatgrab` library.
+
+## Table of Contents
+
+* [Introduction](#introduction)
+* [Compatibility](#compatibility)
+* [Installation](#installation)
+* [Examples](#examples)
+* [Contributing](#contributing)
+* [Licence](#licence)
+
+
+## Introduction
+
+`libstatgrab` is a library that provides cross platform access to statistics
+about the system on which it's run. It's written in C and presents a selection
+of useful interfaces which can be used to access key system statistics.
+The current list of statistics includes CPU usage, memory utilisation, disk usage,
+process counts, network traffic, disk I/O, and more.
+
+The current list of supported and tested platforms includes OSX, FreeBSD, Linux,
+NetBSD, OpenBSD, Solaris, DragonFly BSD, HP-UX and AIX.
+
+Please see the `libstatgrab` [homepage](http://www.i-scream.org/libstatgrab/)
+for more information.
+
+
+## Compatibility
+
+`libstatgrab 0.9.0` is required.
+
+While the bindings should correctly build on the same platforms as supported by
+the library, only OSX and Linux have currently been tested.
+
+
+## Installation
+
+Install `libstatgrab 0.9.0` from one of the mirrors: http://www.i-scream.org/libstatgrab/
+
+```
+cabal install
+```
+
+
+## Examples
+
+```haskell
+module Main (main) where
+
+import Control.Monad.IO.Class
+import System.Statgrab
+
+main :: IO ()
+main = runStats $ do
+    h <- snapshot :: Stats Host
+    liftIO $ print h
+```
+
+
+## Contributing
+
+For any problems, comments or feedback please create an issue [here on GitHub](github.com/brendanhay/statgrab/issues).
+
+
+## Licence
+
+statgrab is released under the [Mozilla Public License Version 2.0](http://www.mozilla.org/MPL/)
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/src/System/Statgrab.hs b/src/System/Statgrab.hs
new file mode 100644
--- /dev/null
+++ b/src/System/Statgrab.hs
@@ -0,0 +1,106 @@
+{-# LANGUAGE FlexibleContexts           #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+
+-- Module      : System.Statgrab
+-- Copyright   : (c) 2013 Brendan Hay <brendan.g.hay@gmail.com>
+-- License     : This Source Code Form is subject to the terms of
+--               the Mozilla Public License, v. 2.0.
+--               A copy of the MPL can be found in the LICENSE file or
+--               you can obtain it at http://mozilla.org/MPL/2.0/.
+-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
+-- Stability   : experimental
+-- Portability : non-portable (GHC extensions)
+
+-- | Monadic context and data types for managing the underlying libstatgrab FFI calls
+-- with transparent resource allocation and deallocation.
+module System.Statgrab
+    (
+    -- * Running the @Stats@ Monad
+      Stats
+    , runStats
+    , async
+
+     -- * Retrieving Statistics
+    , snapshot
+    , Stat
+    , Struct
+
+    -- * Statistic Types
+    , Host             (..)
+    , CPU              (..)
+    , CPUPercent       (..)
+    , Memory           (..)
+    , Load             (..)
+    , User             (..)
+    , Swap             (..)
+    , FileSystem       (..)
+    , DiskIO           (..)
+    , NetworkIO        (..)
+    , NetworkInterface (..)
+    , Page             (..)
+    , Process          (..)
+    , ProcessCount     (..)
+
+    -- * Enums
+    , HostState        (..)
+    , CPUPercentSource (..)
+    , DeviceType       (..)
+    , InterfaceMode    (..)
+    , InterfaceStatus  (..)
+    , ProcessState     (..)
+    , ProcessSource    (..)
+
+    -- * Re-exported
+    , Async
+    , wait
+    ) where
+
+import           Control.Applicative
+import           Control.Concurrent.Async   (Async, wait)
+import qualified Control.Concurrent.Async   as Async
+import qualified Control.Exception          as E
+import           Control.Monad
+import           Control.Monad.CatchIO
+import           Control.Monad.IO.Class
+import           Control.Monad.Trans.Reader
+import           Data.IORef
+import           GHC.Word
+import           System.Statgrab.Base
+import           System.Statgrab.Internal
+
+newtype Stats a = Stats { unwrap :: ReaderT (IORef Word) IO a }
+    deriving (Applicative, Functor, Monad, MonadIO, MonadCatchIO)
+
+-- | Run the 'Stats' Monad, bracketing libstatgrab's sg_init and sg_shutdown
+-- calls via reference counting.
+runStats :: MonadCatchIO m => Stats a -> m a
+runStats = liftIO
+    . bracket (sg_init 0 >> sg_drop_privileges >> newIORef 1) destroy
+    . runReaderT
+    . unwrap
+
+-- | Run the 'Stats' Monad asynchronously. 'wait' from the async package can
+-- be used to block and retrieve the result of the asynchronous computation.
+async :: Stats a -> Stats (Async a)
+async (Stats s) = Stats $ do
+    ref <- ask
+    liftIO $ do
+        atomicModifyIORef' ref $ \ n -> (succ n, ())
+        Async.async $ runReaderT s ref `E.finally` destroy ref
+
+-- | Retrieve statistics from the underlying operating system, copying them to
+-- the Haskell heap and freeing the related @Ptr a@.
+--
+-- The *_r variants of the libstatgrab functions are used and
+-- the deallocation strategy is bracketed.
+snapshot :: (Stat (Struct a), Copy a) => Stats a
+snapshot = liftIO $ bracket acquire release copy
+
+--
+-- Internal
+--
+
+destroy :: IORef Word -> IO ()
+destroy ref = do
+    n <- atomicModifyIORef' ref $ \n -> (pred n, n)
+    when (n == 1) $ void sg_shutdown
diff --git a/src/System/Statgrab/Base.hsc b/src/System/Statgrab/Base.hsc
new file mode 100644
--- /dev/null
+++ b/src/System/Statgrab/Base.hsc
@@ -0,0 +1,1161 @@
+{-# LANGUAGE CPP                        #-}
+{-# LANGUAGE FlexibleInstances          #-}
+{-# LANGUAGE ForeignFunctionInterface   #-}
+{-# LANGUAGE RecordWildCards            #-}
+{-# LANGUAGE TypeFamilies               #-}
+
+-- Module      : System.Statgrab.Base
+-- Copyright   : (c) 2013 Brendan Hay <brendan.g.hay@gmail.com>
+-- License     : This Source Code Form is subject to the terms of
+--               the Mozilla Public License, v. 2.0.
+--               A copy of the MPL can be found in the LICENSE file or
+--               you can obtain it at http://mozilla.org/MPL/2.0/.
+-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
+-- Stability   : experimental
+-- Portability : non-portable (GHC extensions)
+
+-- | Bindings to the libstatgrab library.
+module System.Statgrab.Base where
+
+import Control.Applicative
+import Data.ByteString          (packCString)
+import Foreign
+import Foreign.C.String
+import Foreign.C.Types
+import System.Statgrab.Internal
+
+data family Struct a
+
+class Copy a where
+    copy :: Ptr (Struct a) -> IO a
+
+class Stat a where
+    acquire :: IO (Ptr a)
+    release :: Ptr a -> IO Error
+
+type ErrorDetailsPtr     = Ptr ErrorDetails
+type HostPtr             = Ptr (Struct Host)
+type CPUPtr              = Ptr (Struct CPU)
+type CPUPercentPtr       = Ptr (Struct CPUPercent)
+type MemoryPtr           = Ptr (Struct Memory)
+type LoadPtr             = Ptr (Struct Load)
+type UserPtr             = Ptr (Struct User)
+type SwapPtr             = Ptr (Struct Swap)
+type FileSystemPtr       = Ptr (Struct FileSystem)
+type DiskIOPtr           = Ptr (Struct DiskIO)
+type NetworkIOPtr        = Ptr (Struct NetworkIO)
+type NetworkInterfacePtr = Ptr (Struct NetworkInterface)
+type PagePtr             = Ptr (Struct Page)
+type ProcessPtr          = Ptr (Struct Process)
+type ProcessCountPtr     = Ptr (Struct ProcessCount)
+
+#include <statgrab.h>
+
+#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
+
+#{enum Error, Error
+    , errNone               = SG_ERROR_NONE
+    , errAsPrintf           = SG_ERROR_ASPRINTF
+    , errDevices            = SG_ERROR_DEVICES
+    , errDevStatGetDevs     = SG_ERROR_DEVSTAT_GETDEVS
+    , errDevstat_selectdevs = SG_ERROR_DEVSTAT_SELECTDEVS
+    , errDiskinfo           = SG_ERROR_DISKINFO
+    , errEnoent             = SG_ERROR_ENOENT
+    , errGetifaddrs         = SG_ERROR_GETIFADDRS
+    , errGetmountInfo       = SG_ERROR_GETMNTINFO
+    , errGetPageSize        = SG_ERROR_GETPAGESIZE
+    , errHost               = SG_ERROR_HOST
+    , errKstatDataLookup    = SG_ERROR_KSTAT_DATA_LOOKUP
+    , errKstatLookup        = SG_ERROR_KSTAT_LOOKUP
+    , errKstatOpen          = SG_ERROR_KSTAT_OPEN
+    , errKstatRead          = SG_ERROR_KSTAT_READ
+    , errKvmGetSwapInfo     = SG_ERROR_KVM_GETSWAPINFO
+    , errKvmOpenFiles       = SG_ERROR_KVM_OPENFILES
+    , errMalloc             = SG_ERROR_MALLOC
+    , errMemStatus          = SG_ERROR_MEMSTATUS
+    , errOpen               = SG_ERROR_OPEN
+    , errOpenDir            = SG_ERROR_OPENDIR
+    , errParse              = SG_ERROR_PARSE
+    , errPdhAdd             = SG_ERROR_PDHADD
+    , errPdhCollect         = SG_ERROR_PDHCOLLECT
+    , errPdhOpen            = SG_ERROR_PDHOPEN
+    , errPdhRead            = SG_ERROR_PDHREAD
+    , errPermission         = SG_ERROR_PERMISSION
+    , errPStat              = SG_ERROR_PSTAT
+    , errSetEGid            = SG_ERROR_SETEGID
+    , errSetEUid            = SG_ERROR_SETEUID
+    , errSetMntent          = SG_ERROR_SETMNTENT
+    , errSocket             = SG_ERROR_SOCKET
+    , errSwapCtl            = SG_ERROR_SWAPCTL
+    , errSysConf            = SG_ERROR_SYSCONF
+    , errSysCtl             = SG_ERROR_SYSCTL
+    , errSysCtlByName       = SG_ERROR_SYSCTLBYNAME
+    , errSysctlNameToMib    = SG_ERROR_SYSCTLNAMETOMIB
+    , errUname              = SG_ERROR_UNAME
+    , errUnsupported        = SG_ERROR_UNSUPPORTED
+    , errXswVerMismatch     = SG_ERROR_XSW_VER_MISMATCH
+    , errGetMsg             = SG_ERROR_GETMSG
+    , errPutMsg             = SG_ERROR_PUTMSG
+    , errInitialisation     = SG_ERROR_INITIALISATION
+    , errMutexLock          = SG_ERROR_MUTEX_LOCK
+    , errMutexUnlock        = SG_ERROR_MUTEX_UNLOCK
+    }
+
+data ErrorDetails = ErrorDetails
+    { erError :: {-# UNPACK #-} !Error
+    , erValue :: {-# UNPACK #-} !CInt
+    , erArg   :: {-# UNPACK #-} !CString
+    }
+
+foreign import ccall safe "statgrab.h sg_get_error"
+     sg_get_error :: IO Error
+
+foreign import ccall safe "statgrab.h sg_get_error_arg"
+     sg_get_error_arg :: IO CString
+
+foreign import ccall safe "statgrab.h sg_get_error_errno"
+     sg_get_error_errno :: IO CInt
+
+foreign import ccall safe "statgrab.h sg_get_error_details"
+     sg_get_error_details :: ErrorDetailsPtr -> IO Error
+
+foreign import ccall safe "statgrab.h sg_str_error"
+     sg_str_error :: Error -> IO CString
+
+foreign import ccall safe "statgrab.h sg_strperror"
+     sg_strperror :: Ptr CString -> ErrorDetailsPtr -> IO CString
+
+foreign import ccall safe "statgrab.h sg_init"
+     sg_init :: CInt -> IO Error
+
+foreign import ccall safe "statgrab.h sg_snapshot"
+     sg_snapshot :: IO Error
+
+foreign import ccall safe "statgrab.h sg_shutdown"
+     sg_shutdown :: IO Error
+
+foreign import ccall safe "statgrab.h sg_drop_privileges"
+     sg_drop_privileges :: IO Error
+
+foreign import ccall safe "statgrab.h sg_get_nelements"
+     sg_get_nelements :: Ptr () -> IO CSize
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+     sg_free_stats_buf :: Ptr () -> IO Error
+
+foreign import ccall safe "statgrab.h sg_lock_mutex"
+     sg_lock_mutex :: CString -> IO Error
+
+foreign import ccall safe "statgrab.h sg_unlock_mutex"
+     sg_unlock_mutex :: CString -> IO Error
+
+#{enum HostState, HostState
+    , stateUnknownConfig   = sg_unknown_configuration
+    , statePhysicalHost    = sg_physical_host
+    , stateVirtual         = sg_virtual_machine
+    , stateParaVirtual     = sg_paravirtual_machine
+    , stateHardwareVirtual = sg_hardware_virtualized
+    }
+
+data instance Struct Host = CHost
+    { hostOsName    :: {-# UNPACK #-} !CString
+    , hostOsRelease :: {-# UNPACK #-} !CString
+    , hostOsVersion :: {-# UNPACK #-} !CString
+    , hostPlatform  :: {-# UNPACK #-} !CString
+    , hostName      :: {-# UNPACK #-} !CString
+    , hostBitWidth  :: {-# UNPACK #-} !CUInt
+    , hostState     :: {-# UNPACK #-} !HostState
+    , hostNCPU      :: {-# UNPACK #-} !CUInt
+    , hostMaxCPU    :: {-# UNPACK #-} !CUInt
+    , hostUptime    :: {-# UNPACK #-} !CTime
+    , hostSystime   :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Host where
+    copy ptr = do
+        CHost{..} <- peek ptr
+        Host <$> packCString hostOsName
+             <*> packCString hostOsRelease
+             <*> packCString hostOsVersion
+             <*> packCString hostPlatform
+             <*> packCString hostName
+             <#> hostBitWidth
+             <!> hostState
+             <#> hostNCPU
+             <#> hostMaxCPU
+             <@> hostUptime
+             <@> hostSystime
+
+instance Storable (Struct Host) where
+    alignment _ = #{alignment sg_host_info}
+    sizeOf    _ = #{size      sg_host_info}
+
+    peek p = CHost
+        <$> #{peek sg_host_info, os_name} p
+        <*> #{peek sg_host_info, os_release} p
+        <*> #{peek sg_host_info, os_version} p
+        <*> #{peek sg_host_info, platform} p
+        <*> #{peek sg_host_info, hostname} p
+        <*> #{peek sg_host_info, bitwidth} p
+        <*> #{peek sg_host_info, host_state} p
+        <*> #{peek sg_host_info, ncpus} p
+        <*> #{peek sg_host_info, maxcpus} p
+        <*> #{peek sg_host_info, uptime} p
+        <*> #{peek sg_host_info, systime} p
+
+    poke p CHost{..} = do
+        #{poke sg_host_info, os_name} p hostOsName
+        #{poke sg_host_info, os_release} p hostOsRelease
+        #{poke sg_host_info, os_version} p hostOsVersion
+        #{poke sg_host_info, platform} p hostPlatform
+        #{poke sg_host_info, hostname} p hostName
+        #{poke sg_host_info, bitwidth} p hostBitWidth
+        #{poke sg_host_info, host_state} p hostState
+        #{poke sg_host_info, ncpus} p hostNCPU
+        #{poke sg_host_info, maxcpus} p hostMaxCPU
+        #{poke sg_host_info, uptime} p hostUptime
+        #{poke sg_host_info, systime} p hostSystime
+
+instance Stat (Struct Host) where
+    acquire = alloca sg_get_host_info_r
+    release = sg_free_host_info
+
+foreign import ccall safe "statgrab.h sg_get_host_info"
+    sg_get_host_info :: Entries -> IO HostPtr
+
+foreign import ccall safe "statgrab.h sg_get_host_info_r"
+    sg_get_host_info_r :: Entries -> IO HostPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_host_info :: HostPtr -> IO Error
+
+data instance Struct CPU = CCPU
+    { cpuUser                   :: {-# UNPACK #-} !CLLong
+    , cpuKernel                 :: {-# UNPACK #-} !CLLong
+    , cpuIdle                   :: {-# UNPACK #-} !CLLong
+    , cpuIOWait                 :: {-# UNPACK #-} !CLLong
+    , cpuSwap                   :: {-# UNPACK #-} !CLLong
+    , cpuNice                   :: {-# UNPACK #-} !CLLong
+    , cpuTotal                  :: {-# UNPACK #-} !CLLong
+    , cpuCtxSwitches            :: {-# UNPACK #-} !CLLong
+    , cpuVoluntaryCtxSwitches   :: {-# UNPACK #-} !CLLong
+    , cpuInvoluntaryCtxSwitches :: {-# UNPACK #-} !CLLong
+    , cpuSyscalls               :: {-# UNPACK #-} !CLLong
+    , cpuInterrupts             :: {-# UNPACK #-} !CLLong
+    , cpuSoftInterrupts         :: {-# UNPACK #-} !CLLong
+    , cpuSystime                :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy CPU where
+    copy ptr = do
+        CCPU{..} <- peek ptr
+        CPU <%> cpuUser
+            <#> cpuKernel
+            <#> cpuIdle
+            <#> cpuIOWait
+            <#> cpuSwap
+            <#> cpuNice
+            <#> cpuTotal
+            <#> cpuCtxSwitches
+            <#> cpuVoluntaryCtxSwitches
+            <#> cpuInvoluntaryCtxSwitches
+            <#> cpuSyscalls
+            <#> cpuInterrupts
+            <#> cpuSoftInterrupts
+            <@> cpuSystime
+
+instance Storable (Struct CPU) where
+    alignment _ = #{alignment sg_cpu_stats}
+    sizeOf    _ = #{size      sg_cpu_stats}
+
+    peek p = CCPU
+        <$> #{peek sg_cpu_stats, user} p
+        <*> #{peek sg_cpu_stats, kernel} p
+        <*> #{peek sg_cpu_stats, idle} p
+        <*> #{peek sg_cpu_stats, iowait} p
+        <*> #{peek sg_cpu_stats, swap} p
+        <*> #{peek sg_cpu_stats, nice} p
+        <*> #{peek sg_cpu_stats, total} p
+        <*> #{peek sg_cpu_stats, context_switches} p
+        <*> #{peek sg_cpu_stats, voluntary_context_switches} p
+        <*> #{peek sg_cpu_stats, involuntary_context_switches} p
+        <*> #{peek sg_cpu_stats, syscalls} p
+        <*> #{peek sg_cpu_stats, interrupts} p
+        <*> #{peek sg_cpu_stats, soft_interrupts} p
+        <*> #{peek sg_cpu_stats, systime} p
+
+    poke p CCPU{..} = do
+        #{poke sg_cpu_stats, user} p cpuUser
+        #{poke sg_cpu_stats, kernel} p cpuKernel
+        #{poke sg_cpu_stats, idle} p cpuIdle
+        #{poke sg_cpu_stats, iowait} p cpuIOWait
+        #{poke sg_cpu_stats, swap} p cpuSwap
+        #{poke sg_cpu_stats, nice} p cpuNice
+        #{poke sg_cpu_stats, total} p cpuTotal
+        #{poke sg_cpu_stats, context_switches} p cpuCtxSwitches
+        #{poke sg_cpu_stats, voluntary_context_switches} p cpuVoluntaryCtxSwitches
+        #{poke sg_cpu_stats, involuntary_context_switches} p cpuInvoluntaryCtxSwitches
+        #{poke sg_cpu_stats, syscalls} p cpuSyscalls
+        #{poke sg_cpu_stats, interrupts} p cpuInterrupts
+        #{poke sg_cpu_stats, soft_interrupts} p cpuSoftInterrupts
+        #{poke sg_cpu_stats, systime} p cpuSystime
+
+instance Stat (Struct CPU) where
+    acquire = alloca sg_get_cpu_stats_r
+    release = sg_free_cpu_stats
+
+foreign import ccall safe "statgrab.h sg_get_cpu_stats"
+    sg_get_cpu_stats :: Entries -> IO CPUPtr
+
+foreign import ccall safe "statgrab.h sg_get_cpu_stats_diff"
+    sg_get_cpu_stats_diff :: Entries -> IO CPUPtr
+
+foreign import ccall safe "statgrab.h sg_get_cpu_stats_r"
+    sg_get_cpu_stats_r :: Entries -> IO CPUPtr
+
+foreign import ccall safe "statgrab.h sg_get_cpu_stats_diff_between"
+    sg_get_cpu_stats_diff_between :: CPUPtr -> CPUPtr -> Entries -> IO CPUPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_cpu_stats :: CPUPtr -> IO Error
+
+data instance Struct CPUPercent = CCPUPercent
+    { cpuPctUser      :: {-# UNPACK #-} !CDouble
+    , cpuPctKernel    :: {-# UNPACK #-} !CDouble
+    , cpuPctIdle      :: {-# UNPACK #-} !CDouble
+    , cpuPctIOWait    :: {-# UNPACK #-} !CDouble
+    , cpuPctSwap      :: {-# UNPACK #-} !CDouble
+    , cpuPctNice      :: {-# UNPACK #-} !CDouble
+    , cpuPctTimeTaken :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy CPUPercent where
+    copy ptr = do
+        CCPUPercent{..} <- peek ptr
+        CPUPercent <$> pure (realToFrac cpuPctUser)
+                   <@> cpuPctKernel
+                   <@> cpuPctIdle
+                   <@> cpuPctIOWait
+                   <@> cpuPctSwap
+                   <@> cpuPctNice
+                   <@> cpuPctTimeTaken
+
+instance Storable (Struct CPUPercent) where
+    alignment _ = #{alignment sg_cpu_percents}
+    sizeOf    _ = #{size      sg_cpu_percents}
+
+    peek p = CCPUPercent
+        <$> #{peek sg_cpu_percents, user} p
+        <*> #{peek sg_cpu_percents, kernel} p
+        <*> #{peek sg_cpu_percents, idle} p
+        <*> #{peek sg_cpu_percents, iowait} p
+        <*> #{peek sg_cpu_percents, swap} p
+        <*> #{peek sg_cpu_percents, nice} p
+        <*> #{peek sg_cpu_percents, time_taken} p
+
+    poke p CCPUPercent{..} = do
+        #{poke sg_cpu_percents, user} p cpuPctUser
+        #{poke sg_cpu_percents, kernel} p cpuPctKernel
+        #{poke sg_cpu_percents, idle} p cpuPctIdle
+        #{poke sg_cpu_percents, iowait} p cpuPctIOWait
+        #{poke sg_cpu_percents, swap} p cpuPctSwap
+        #{poke sg_cpu_percents, nice} p cpuPctNice
+        #{poke sg_cpu_percents, time_taken} p cpuPctTimeTaken
+
+#{enum CPUPercentSource, CPUPercentSource
+    , sourceEntireCCPU = sg_entire_cpu_percent
+    , sourceDiffCCPU   = sg_last_diff_cpu_percent
+    , sourceNewDiffCCPU= sg_new_diff_cpu_percent
+}
+
+foreign import ccall safe "statgrab.h sg_get_cpu_percents_of"
+    sg_get_cpu_percents_of :: CPUPercentSource -> Entries -> IO CPUPercentPtr
+
+foreign import ccall safe "statgrab.h sg_get_cpu_percents_r"
+    sg_get_cpu_percents_r :: CPUPtr -> Entries -> IO CPUPercentPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_cpu_percents :: CPUPercentPtr -> IO Error
+
+data instance Struct Memory = CMemory
+    { memTotal   :: {-# UNPACK #-} !CULLong
+    , memFree    :: {-# UNPACK #-} !CULLong
+    , memUsed    :: {-# UNPACK #-} !CULLong
+    , memCache   :: {-# UNPACK #-} !CULLong
+    , memSystime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Memory where
+    copy ptr = do
+        CMemory{..} <- peek ptr
+        Memory <%> memTotal
+               <#> memFree
+               <#> memUsed
+               <#> memCache
+               <@> memSystime
+
+instance Storable (Struct Memory) where
+    alignment _ = #{alignment sg_mem_stats}
+    sizeOf    _ = #{size      sg_mem_stats}
+
+    peek p = CMemory
+        <$> #{peek sg_mem_stats, total} p
+        <*> #{peek sg_mem_stats, free} p
+        <*> #{peek sg_mem_stats, used} p
+        <*> #{peek sg_mem_stats, cache} p
+        <*> #{peek sg_mem_stats, systime} p
+
+    poke p CMemory{..} = do
+        #{poke sg_mem_stats, total} p memTotal
+        #{poke sg_mem_stats, free} p memFree
+        #{poke sg_mem_stats, used} p memUsed
+        #{poke sg_mem_stats, systime} p memSystime
+
+instance Stat (Struct Memory) where
+    acquire = alloca sg_get_mem_stats_r
+    release = sg_free_mem_stats
+
+foreign import ccall safe "statgrab.h sg_get_mem_stats"
+    sg_get_mem_stats :: Entries -> IO MemoryPtr
+
+foreign import ccall safe "statgrab.h sg_get_mem_stats_r"
+    sg_get_mem_stats_r :: Entries -> IO MemoryPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_mem_stats :: MemoryPtr -> IO Error
+
+data instance Struct Load = CLoad
+    { load1       :: {-# UNPACK #-} !CDouble
+    , load5       :: {-# UNPACK #-} !CDouble
+    , load15      :: {-# UNPACK #-} !CDouble
+    , loadSystime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Load where
+    copy ptr = do
+        CLoad{..} <- peek ptr
+        Load <$> pure (realToFrac load1)
+             <@> load5
+             <@> load15
+             <@> loadSystime
+
+instance Storable (Struct Load) where
+    alignment _ = #{alignment sg_load_stats}
+    sizeOf    _ = #{size      sg_load_stats}
+
+    peek p = CLoad
+        <$> #{peek sg_load_stats, min1} p
+        <*> #{peek sg_load_stats, min5} p
+        <*> #{peek sg_load_stats, min15} p
+        <*> #{peek sg_load_stats, systime} p
+
+    poke p CLoad{..} = do
+        #{poke sg_load_stats, min1} p load1
+        #{poke sg_load_stats, min5} p load5
+        #{poke sg_load_stats, min15} p load15
+        #{poke sg_load_stats, systime} p loadSystime
+
+instance Stat (Struct Load) where
+    acquire = alloca sg_get_load_stats_r
+    release = sg_free_load_stats
+
+foreign import ccall safe "statgrab.h sg_get_load_stats"
+    sg_get_load_stats :: Entries -> IO LoadPtr
+
+foreign import ccall safe "statgrab.h sg_get_load_stats_r"
+    sg_get_load_stats_r :: Entries -> IO LoadPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_load_stats :: LoadPtr -> IO Error
+
+data instance Struct User = CUser
+    { userLoginName    :: {-# UNPACK #-} !CString
+    , userRecordId     :: {-# UNPACK #-} !CString
+    , userRecordIdSize :: {-# UNPACK #-} !CSize
+    , userDevice       :: {-# UNPACK #-} !CString
+    , userHostName     :: {-# UNPACK #-} !CString
+    , userPid          :: {-# UNPACK #-} !CInt
+    , userLoginTime    :: {-# UNPACK #-} !CTime
+    , userSystime      :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy User where
+    copy ptr = do
+        CUser{..} <- peek ptr
+        User <$> packCString userLoginName
+             <*> packCString userRecordId
+             <#> userRecordIdSize
+             <*> packCString userDevice
+             <*> packCString userHostName
+             <#> userPid
+             <@> userLoginTime
+             <@> userSystime
+
+instance Storable (Struct User) where
+    alignment _ = #{alignment sg_user_stats}
+    sizeOf    _ = #{size      sg_user_stats}
+
+    peek p = CUser
+        <$> #{peek sg_user_stats, login_name} p
+        <*> #{peek sg_user_stats, record_id} p
+        <*> #{peek sg_user_stats, record_id_size} p
+        <*> #{peek sg_user_stats, device} p
+        <*> #{peek sg_user_stats, hostname} p
+        <*> #{peek sg_user_stats, pid} p
+        <*> #{peek sg_user_stats, login_time} p
+        <*> #{peek sg_user_stats, systime} p
+
+    poke p CUser{..} = do
+        #{poke sg_user_stats, login_name} p userLoginTime
+        #{poke sg_user_stats, record_id} p userRecordId
+        #{poke sg_user_stats, record_id_size} p userRecordIdSize
+        #{poke sg_user_stats, device} p userDevice
+        #{poke sg_user_stats, hostname} p userHostName
+        #{poke sg_user_stats, pid} p userPid
+        #{poke sg_user_stats, login_time} p userLoginTime
+        #{poke sg_user_stats, systime} p userSystime
+
+instance Stat (Struct User) where
+    acquire = alloca sg_get_user_stats_r
+    release = sg_free_user_stats
+
+foreign import ccall safe "statgrab.h sg_get_user_stats"
+    sg_get_user_stats :: Entries -> IO UserPtr
+
+foreign import ccall safe "statgrab.h sg_get_user_stats_r"
+    sg_get_user_stats_r :: Entries -> IO UserPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_user_stats :: UserPtr -> IO Error
+
+data instance Struct Swap = CSwap
+    { swapTotal   :: {-# UNPACK #-} !CULLong
+    , swapUsed    :: {-# UNPACK #-} !CULLong
+    , swapFree    :: {-# UNPACK #-} !CULLong
+    , swapSystime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Swap where
+    copy ptr = do
+        CSwap{..} <- peek ptr
+        Swap <%> swapTotal
+             <#> swapUsed
+             <#> swapFree
+             <@> swapSystime
+
+instance Storable (Struct Swap) where
+    alignment _ = #{alignment sg_swap_stats}
+    sizeOf    _ = #{size      sg_swap_stats}
+
+    peek p = CSwap
+        <$> #{peek sg_swap_stats, total} p
+        <*> #{peek sg_swap_stats, used} p
+        <*> #{peek sg_swap_stats, free} p
+        <*> #{peek sg_swap_stats, systime} p
+
+    poke p CSwap{..} = do
+        #{poke sg_swap_stats, total} p swapTotal
+        #{poke sg_swap_stats, used} p swapUsed
+        #{poke sg_swap_stats, free} p swapFree
+        #{poke sg_swap_stats, systime} p swapSystime
+
+instance Stat (Struct Swap) where
+    acquire = alloca sg_get_swap_stats_r
+    release = sg_free_swap_stats
+
+foreign import ccall safe "statgrab.h sg_get_swap_stats"
+    sg_get_swap_stats :: Entries -> IO SwapPtr
+
+foreign import ccall safe "statgrab.h sg_get_swap_stats_r"
+    sg_get_swap_stats_r :: Entries -> IO SwapPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_swap_stats :: SwapPtr -> IO Error
+
+#{enum DeviceType, DeviceType
+    , deviceUnknown  = sg_fs_unknown
+    , deviceRegular  = sg_fs_regular
+    , deviceSpecial  = sg_fs_special
+    , deviceLoopback = sg_fs_loopback
+    , deviceRemote   = sg_fs_remote
+    , deviceLocal    = sg_fs_local
+    , deviceAllTypes = sg_fs_alltypes
+    }
+
+data instance Struct FileSystem = CFileSystem
+    { fsDeviceName  :: {-# UNPACK #-} !CString
+    , fsType        :: {-# UNPACK #-} !CString
+    , fsMountPoint  :: {-# UNPACK #-} !CString
+    , fsDeviceType  :: {-# UNPACK #-} !DeviceType
+    , fsSize        :: {-# UNPACK #-} !CULLong
+    , fsUsed        :: {-# UNPACK #-} !CULLong
+    , fsFree        :: {-# UNPACK #-} !CULLong
+    , fsAvail       :: {-# UNPACK #-} !CULLong
+    , fsTotalInodes :: {-# UNPACK #-} !CULLong
+    , fsUsedInodes  :: {-# UNPACK #-} !CULLong
+    , fsFreeInodes  :: {-# UNPACK #-} !CULLong
+    , fsAvailInodes :: {-# UNPACK #-} !CULLong
+    , fsIOSize      :: {-# UNPACK #-} !CULLong
+    , fsBlockSize   :: {-# UNPACK #-} !CULLong
+    , fsTotalBlocks :: {-# UNPACK #-} !CULLong
+    , fsFreeBlocks  :: {-# UNPACK #-} !CULLong
+    , fsUsedBlocks  :: {-# UNPACK #-} !CULLong
+    , fsAvailBlocks :: {-# UNPACK #-} !CULLong
+    , fsSystime     :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy FileSystem where
+    copy ptr = do
+        CFileSystem{..} <- peek ptr
+        FileSystem <$> packCString fsDeviceName
+                   <*> packCString fsType
+                   <*> packCString fsMountPoint
+                   <!> fsDeviceType
+                   <#> fsSize
+                   <#> fsUsed
+                   <#> fsFree
+                   <#> fsAvail
+                   <#> fsTotalInodes
+                   <#> fsUsedInodes
+                   <#> fsFreeInodes
+                   <#> fsAvailInodes
+                   <#> fsIOSize
+                   <#> fsBlockSize
+                   <#> fsTotalBlocks
+                   <#> fsFreeBlocks
+                   <#> fsUsedBlocks
+                   <#> fsAvailBlocks
+                   <@> fsSystime
+
+instance Storable (Struct FileSystem) where
+    alignment _ = #{alignment sg_fs_stats}
+    sizeOf    _ = #{size      sg_fs_stats}
+
+    peek p = CFileSystem
+        <$> #{peek sg_fs_stats, device_name} p
+        <*> #{peek sg_fs_stats, fs_type} p
+        <*> #{peek sg_fs_stats, mnt_point} p
+        <*> #{peek sg_fs_stats, device_type} p
+        <*> #{peek sg_fs_stats, size} p
+        <*> #{peek sg_fs_stats, used} p
+        <*> #{peek sg_fs_stats, free} p
+        <*> #{peek sg_fs_stats, avail} p
+        <*> #{peek sg_fs_stats, total_inodes} p
+        <*> #{peek sg_fs_stats, used_inodes} p
+        <*> #{peek sg_fs_stats, free_inodes} p
+        <*> #{peek sg_fs_stats, avail_inodes} p
+        <*> #{peek sg_fs_stats, io_size} p
+        <*> #{peek sg_fs_stats, block_size} p
+        <*> #{peek sg_fs_stats, total_blocks} p
+        <*> #{peek sg_fs_stats, free_blocks} p
+        <*> #{peek sg_fs_stats, used_blocks} p
+        <*> #{peek sg_fs_stats, avail_blocks} p
+        <*> #{peek sg_fs_stats, systime} p
+
+    poke p CFileSystem{..} = do
+        #{poke sg_fs_stats, device_name} p fsDeviceName
+        #{poke sg_fs_stats, fs_type} p fsType
+        #{poke sg_fs_stats, mnt_point} p fsMountPoint
+        #{poke sg_fs_stats, device_type} p fsDeviceType
+        #{poke sg_fs_stats, size} p fsSize
+        #{poke sg_fs_stats, used} p fsUsed
+        #{poke sg_fs_stats, free} p fsFree
+        #{poke sg_fs_stats, avail} p fsAvail
+        #{poke sg_fs_stats, total_inodes} p fsTotalInodes
+        #{poke sg_fs_stats, used_inodes} p fsUsedInodes
+        #{poke sg_fs_stats, free_inodes} p fsFreeInodes
+        #{poke sg_fs_stats, avail_inodes} p fsAvailInodes
+        #{poke sg_fs_stats, io_size} p fsIOSize
+        #{poke sg_fs_stats, block_size} p fsBlockSize
+        #{poke sg_fs_stats, total_blocks} p fsTotalBlocks
+        #{poke sg_fs_stats, free_blocks} p fsFreeBlocks
+        #{poke sg_fs_stats, used_blocks} p fsUsedBlocks
+        #{poke sg_fs_stats, avail_blocks} p fsAvailBlocks
+        #{poke sg_fs_stats, systime} p fsSystime
+
+instance Stat (Struct FileSystem) where
+    acquire = alloca sg_get_fs_stats_r
+    release = sg_free_fs_stats
+
+foreign import ccall safe "statgrab.h sg_get_valid_filesystems"
+     sg_get_valid_filesystems :: Entries -> IO (Ptr CString)
+
+-- foreign import ccall safe "statgrab.h sg_get_fs_val"
+--      sg_set_valid_filesystems :: Ptr CString -> IO Error
+-- __sg_public const char **sg_get_valid_filesystems(size_t *entries);
+-- __sg_public sg_error sg_set_valid_filesystems(const char *valid_fs[]);
+
+foreign import ccall safe "statgrab.h sg_get_fs_stats"
+     sg_get_fs_stats :: Entries -> IO FileSystemPtr
+
+foreign import ccall safe "statgrab.h sg_get_fs_stats_r"
+     sg_get_fs_stats_r :: Entries -> IO FileSystemPtr
+
+foreign import ccall safe "statgrab.h sg_get_fs_stats_diff"
+     sg_get_fs_stats_diff :: Entries -> IO FileSystemPtr
+
+foreign import ccall safe "statgrab.h sg_get_fs_stats_diff_between"
+     sg_get_fs_stats_diff_between :: FileSystemPtr
+                                  -> FileSystemPtr
+                                  -> Entries
+                                  -> IO FileSystemPtr
+
+foreign import ccall safe "statgrab.h sg_fs_compare_device_name"
+     sg_fs_compare_device_name :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_fs_compare_mnt_point"
+     sg_fs_compare_mnt_point :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_fs_stats :: FileSystemPtr -> IO Error
+
+data instance Struct DiskIO = CDiskIO
+    { diskName    :: {-# UNPACK #-} !CString
+    , diskRead    :: {-# UNPACK #-} !CULLong
+    , diskWrite   :: {-# UNPACK #-} !CULLong
+    , diskSystime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy DiskIO where
+    copy ptr = do
+        CDiskIO{..} <- peek ptr
+        DiskIO <$> packCString diskName
+               <#> diskRead
+               <#> diskWrite
+               <@> diskSystime
+
+instance Storable (Struct DiskIO) where
+    alignment _ = #{alignment sg_disk_io_stats}
+    sizeOf    _ = #{size      sg_disk_io_stats}
+
+    peek p = CDiskIO
+        <$> #{peek sg_disk_io_stats, disk_name} p
+        <*> #{peek sg_disk_io_stats, read_bytes} p
+        <*> #{peek sg_disk_io_stats, write_bytes} p
+        <*> #{peek sg_disk_io_stats, systime} p
+
+    poke p CDiskIO{..} = do
+        #{poke sg_disk_io_stats, disk_name} p diskName
+        #{poke sg_disk_io_stats, read_bytes} p diskRead
+        #{poke sg_disk_io_stats, write_bytes} p diskWrite
+        #{poke sg_disk_io_stats, systime} p diskSystime
+
+instance Stat (Struct DiskIO) where
+    acquire = alloca sg_get_disk_io_stats_r
+    release = sg_free_disk_io_stats
+
+foreign import ccall safe "statgrab.h sg_get_disk_io_stats"
+    sg_get_disk_io_stats :: Entries -> IO DiskIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_disk_io_stats_r"
+    sg_get_disk_io_stats_r :: Entries -> IO DiskIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_disk_io_stats_diff"
+    sg_get_disk_io_stats_diff :: Entries -> IO DiskIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_disk_io_stats_diff_between"
+    sg_get_disk_io_stats_diff_between :: DiskIOPtr
+                                      -> DiskIOPtr
+                                      -> Entries
+                                      -> IO DiskIOPtr
+
+foreign import ccall safe "statgrab.h sg_disk_io_compare_name"
+    sg_disk_io_compare_name :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_disk_io_compare_traffic"
+    sg_disk_io_compare_traffic :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_disk_io_stats :: DiskIOPtr -> IO Error
+
+data instance Struct NetworkIO = CNetworkIO
+    { ifaceIOName     :: {-# UNPACK #-} !CString
+    , ifaceTX         :: {-# UNPACK #-} !CULLong
+    , ifaceRX         :: {-# UNPACK #-} !CULLong
+    , ifaceIPackets   :: {-# UNPACK #-} !CULLong
+    , ifaceOPackets   :: {-# UNPACK #-} !CULLong
+    , ifaceIErrors    :: {-# UNPACK #-} !CULLong
+    , ifaceOErrors    :: {-# UNPACK #-} !CULLong
+    , ifaceCollisions :: {-# UNPACK #-} !CULLong
+    , ifaceSystem     :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy NetworkIO where
+    copy ptr = do
+        CNetworkIO{..} <- peek ptr
+        NetworkIO <$> packCString ifaceIOName
+                  <#> ifaceTX
+                  <#> ifaceRX
+                  <#> ifaceIPackets
+                  <#> ifaceOPackets
+                  <#> ifaceIErrors
+                  <#> ifaceOErrors
+                  <#> ifaceCollisions
+                  <@> ifaceSystem
+
+instance Storable (Struct NetworkIO) where
+    alignment _ = #{alignment sg_network_io_stats}
+    sizeOf    _ = #{size      sg_network_io_stats}
+
+    peek p = CNetworkIO
+        <$> #{peek sg_network_io_stats, interface_name} p
+        <*> #{peek sg_network_io_stats, tx} p
+        <*> #{peek sg_network_io_stats, rx} p
+        <*> #{peek sg_network_io_stats, ipackets} p
+        <*> #{peek sg_network_io_stats, opackets} p
+        <*> #{peek sg_network_io_stats, ierrors} p
+        <*> #{peek sg_network_io_stats, oerrors} p
+        <*> #{peek sg_network_io_stats, collisions} p
+        <*> #{peek sg_network_io_stats, systime} p
+
+    poke p CNetworkIO{..} = do
+        #{poke sg_network_io_stats, interface_name} p ifaceIOName
+        #{poke sg_network_io_stats, tx} p ifaceTX
+        #{poke sg_network_io_stats, rx} p ifaceRX
+        #{poke sg_network_io_stats, ipackets} p ifaceIPackets
+        #{poke sg_network_io_stats, opackets} p ifaceOPackets
+        #{poke sg_network_io_stats, ierrors} p ifaceIErrors
+        #{poke sg_network_io_stats, oerrors} p ifaceOErrors
+        #{poke sg_network_io_stats, collisions} p ifaceCollisions
+        #{poke sg_network_io_stats, systime} p ifaceSystem
+
+instance Stat (Struct NetworkIO) where
+    acquire = alloca sg_get_network_io_stats_r
+    release = sg_free_network_io_stats
+
+foreign import ccall safe "statgrab.h sg_get_network_io_stats"
+     sg_get_network_io_stats :: Entries -> IO NetworkIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_network_io_stats_r"
+     sg_get_network_io_stats_r :: Entries -> IO NetworkIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_network_io_stats_diff"
+     sg_get_network_io_stats_diff :: Entries -> IO NetworkIOPtr
+
+foreign import ccall safe "statgrab.h sg_get_network_io_stats_diff_between"
+     sg_get_network_io_stats_diff_between :: NetworkIOPtr
+                                          -> NetworkIOPtr
+                                          -> Entries
+                                          -> IO NetworkIOPtr
+
+foreign import ccall safe "statgrab.h sg_network_io_compare_name"
+     sg_network_io_compare_name :: Ptr () -> Ptr () -> IO (CInt)
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_network_io_stats :: NetworkIOPtr -> IO Error
+
+#{enum InterfaceMode, InterfaceMode
+    , duplexFull    = SG_IFACE_DUPLEX_FULL
+    , duplexHalf    = SG_IFACE_DUPLEX_HALF
+    , duplexUnknown = SG_IFACE_DUPLEX_UNKNOWN
+    }
+
+#{enum InterfaceStatus, InterfaceStatus
+    , statusDown = SG_IFACE_DOWN
+    , statusUp   = SG_IFACE_UP
+    }
+
+data instance Struct NetworkInterface = CNetworkInterface
+    { ifaceName    :: {-# UNPACK #-} !CString
+    , ifaceSpeed   :: {-# UNPACK #-} !CULLong
+    , ifaceFactor  :: {-# UNPACK #-} !CULLong
+    , ifaceDuplex  :: {-# UNPACK #-} !InterfaceMode
+    , ifaceUp      :: {-# UNPACK #-} !InterfaceStatus
+    , ifaceSystime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy NetworkInterface where
+    copy ptr = do
+        CNetworkInterface{..} <- peek ptr
+        NetworkInterface <$> packCString ifaceName
+                         <#> ifaceSpeed
+                         <#> ifaceFactor
+                         <!> ifaceDuplex
+                         <!> ifaceUp
+                         <@> ifaceSystime
+
+instance Storable (Struct NetworkInterface) where
+    alignment _ = #{alignment sg_network_iface_stats}
+    sizeOf    _ = #{size      sg_network_iface_stats}
+
+    peek p = CNetworkInterface
+        <$> #{peek sg_network_iface_stats, interface_name} p
+        <*> #{peek sg_network_iface_stats, speed} p
+        <*> #{peek sg_network_iface_stats, factor} p
+        <*> #{peek sg_network_iface_stats, duplex} p
+        <*> #{peek sg_network_iface_stats, up} p
+        <*> #{peek sg_network_iface_stats, systime} p
+
+    poke p CNetworkInterface{..} = do
+        #{poke sg_network_iface_stats, interface_name} p ifaceName
+        #{poke sg_network_iface_stats, speed} p ifaceSpeed
+        #{poke sg_network_iface_stats, factor} p ifaceFactor
+        #{poke sg_network_iface_stats, duplex} p ifaceDuplex
+        #{poke sg_network_iface_stats, up} p ifaceUp
+        #{poke sg_network_iface_stats, systime} p ifaceSystime
+
+instance Stat (Struct NetworkInterface) where
+    acquire = alloca sg_get_network_iface_stats_r
+    release = sg_free_network_iface_stats
+
+foreign import ccall safe "statgrab.h sg_get_network_iface_stats"
+    sg_get_network_iface_stats :: Entries -> IO NetworkInterfacePtr
+
+foreign import ccall safe "statgrab.h sg_get_network_iface_stats_r"
+    sg_get_network_iface_stats_r :: Entries -> IO NetworkInterfacePtr
+
+foreign import ccall safe "statgrab.h sg_network_iface_compare_name"
+    sg_network_iface_compare_name :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_network_iface_stats :: NetworkInterfacePtr -> IO Error
+
+data instance Struct Page = CPage
+    { pagesIn      :: {-# UNPACK #-} !CULLong
+    , pagesOut     :: {-# UNPACK #-} !CULLong
+    , pagesSysTime :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Page where
+    copy ptr = do
+        CPage{..} <- peek ptr
+        Page <%> pagesIn
+             <#> pagesOut
+             <@> pagesSysTime
+
+instance Storable (Struct Page) where
+    alignment _ = #{alignment sg_page_stats}
+    sizeOf    _ = #{size      sg_page_stats}
+
+    peek p = CPage
+        <$> #{peek sg_page_stats, pages_pagein} p
+        <*> #{peek sg_page_stats, pages_pageout} p
+        <*> #{peek sg_page_stats, systime} p
+
+    poke p CPage{..} = do
+        #{poke sg_page_stats, pages_pagein} p pagesIn
+        #{poke sg_page_stats, pages_pageout} p pagesOut
+        #{poke sg_page_stats, systime} p pagesSysTime
+
+instance Stat (Struct Page) where
+    acquire = alloca sg_get_page_stats_r
+    release = sg_free_page_stats
+
+foreign import ccall safe "statgrab.h sg_get_page_stats"
+     sg_get_page_stats :: Entries -> IO PagePtr
+
+foreign import ccall safe "statgrab.h sg_get_page_stats_r"
+     sg_get_page_stats_r :: Entries -> IO PagePtr
+
+foreign import ccall safe "statgrab.h sg_get_page_stats_diff"
+     sg_get_page_stats_diff :: Entries -> IO PagePtr
+
+foreign import ccall safe "statgrab.h sg_get_page_stats_diff_between"
+     sg_get_page_stats_diff_between :: PagePtr
+                                    -> PagePtr
+                                    -> Entries
+                                    -> IO PagePtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_page_stats :: PagePtr -> IO Error
+
+#{enum ProcessState, ProcessState
+    , stateRunning  = SG_PROCESS_STATE_RUNNING
+    , stateSleeping = SG_PROCESS_STATE_SLEEPING
+    , stateStopped  = SG_PROCESS_STATE_STOPPED
+    , stateZombie   = SG_PROCESS_STATE_ZOMBIE
+    , stateUnknown  = SG_PROCESS_STATE_UNKNOWN
+    }
+
+data instance Struct Process = CProcess
+    { procName        :: {-# UNPACK #-} !CString
+    , procTitle       :: {-# UNPACK #-} !CString
+    , procPid         :: {-# UNPACK #-} !CInt
+    , procParent      :: {-# UNPACK #-} !CInt
+    , procPGid        :: {-# UNPACK #-} !CInt
+    , procSessId      :: {-# UNPACK #-} !CInt
+    , procUid         :: {-# UNPACK #-} !CUInt
+    , procEUid        :: {-# UNPACK #-} !CUInt
+    , procGid         :: {-# UNPACK #-} !CUInt
+    , procEGid        :: {-# UNPACK #-} !CUInt
+    , procSwitches    :: {-# UNPACK #-} !CULLong
+    , procVoluntary   :: {-# UNPACK #-} !CULLong
+    , procInvoluntary :: {-# UNPACK #-} !CULLong
+    , procSize        :: {-# UNPACK #-} !CULLong
+    , procResident    :: {-# UNPACK #-} !CULLong
+    , procStart       :: {-# UNPACK #-} !CTime
+    , procSpent       :: {-# UNPACK #-} !CTime
+    , procCPUPercent  :: {-# UNPACK #-} !CDouble
+    , procNice        :: {-# UNPACK #-} !CInt
+    , procState       :: {-# UNPACK #-} !ProcessState
+    , procSystime     :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy Process where
+    copy ptr = do
+        CProcess{..} <- peek ptr
+        Process <$> packCString procName
+                <*> packCString procTitle
+                <#> procPid
+                <#> procParent
+                <#> procPGid
+                <#> procSessId
+                <#> procUid
+                <#> procEUid
+                <#> procGid
+                <#> procEGid
+                <#> procSwitches
+                <#> procVoluntary
+                <#> procInvoluntary
+                <#> procSize
+                <#> procResident
+                <@> procStart
+                <@> procSpent
+                <@> procCPUPercent
+                <#> procNice
+                <!> procState
+                <@> procSystime
+
+instance Storable (Struct Process) where
+    alignment _ = #{alignment sg_process_stats}
+    sizeOf    _ = #{size      sg_process_stats}
+
+    peek p = CProcess
+        <$> #{peek sg_process_stats, process_name} p
+        <*> #{peek sg_process_stats, proctitle} p
+        <*> #{peek sg_process_stats, pid} p
+        <*> #{peek sg_process_stats, parent} p
+        <*> #{peek sg_process_stats, pgid} p
+        <*> #{peek sg_process_stats, sessid} p
+        <*> #{peek sg_process_stats, uid} p
+        <*> #{peek sg_process_stats, euid} p
+        <*> #{peek sg_process_stats, gid} p
+        <*> #{peek sg_process_stats, egid} p
+        <*> #{peek sg_process_stats, context_switches} p
+        <*> #{peek sg_process_stats, voluntary_context_switches} p
+        <*> #{peek sg_process_stats, involuntary_context_switches} p
+        <*> #{peek sg_process_stats, proc_size} p
+        <*> #{peek sg_process_stats, proc_resident} p
+        <*> #{peek sg_process_stats, start_time} p
+        <*> #{peek sg_process_stats, time_spent} p
+        <*> #{peek sg_process_stats, cpu_percent} p
+        <*> #{peek sg_process_stats, nice} p
+        <*> #{peek sg_process_stats, state} p
+        <*> #{peek sg_process_stats, systime} p
+
+    poke p CProcess{..} = do
+        #{poke sg_process_stats, process_name} p procName
+        #{poke sg_process_stats, proctitle} p procTitle
+        #{poke sg_process_stats, pid} p procPid
+        #{poke sg_process_stats, parent} p procParent
+        #{poke sg_process_stats, pgid} p procPGid
+        #{poke sg_process_stats, sessid} p procSessId
+        #{poke sg_process_stats, uid} p procUid
+        #{poke sg_process_stats, euid} p procEUid
+        #{poke sg_process_stats, gid} p procGid
+        #{poke sg_process_stats, egid} p procEGid
+        #{poke sg_process_stats, context_switches} p procSwitches
+        #{poke sg_process_stats, voluntary_context_switches} p procVoluntary
+        #{poke sg_process_stats, involuntary_context_switches} p procInvoluntary
+        #{poke sg_process_stats, proc_size} p procSize
+        #{poke sg_process_stats, proc_resident} p procResident
+        #{poke sg_process_stats, start_time} p procStart
+        #{poke sg_process_stats, time_spent} p procSpent
+        #{poke sg_process_stats, cpu_percent} p procCPUPercent
+        #{poke sg_process_stats, nice} p procNice
+        #{poke sg_process_stats, state} p procState
+        #{poke sg_process_stats, systime} p procSystime
+
+instance Stat (Struct Process) where
+    acquire = alloca sg_get_process_stats_r
+    release = sg_free_process_stats
+
+foreign import ccall safe "statgrab.h sg_get_process_stats"
+    sg_get_process_stats :: Entries -> IO ProcessPtr
+
+foreign import ccall safe "statgrab.h sg_get_process_stats_r"
+    sg_get_process_stats_r :: Entries -> IO ProcessPtr
+
+foreign import ccall safe "statgrab.h sg_process_compare_name"
+    sg_process_compare_name :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_pid"
+    sg_process_compare_pid :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_uid"
+    sg_process_compare_uid :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_gid"
+    sg_process_compare_gid :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_size"
+    sg_process_compare_size :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_res"
+    sg_process_compare_res :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_cpu"
+    sg_process_compare_cpu :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_process_compare_time"
+    sg_process_compare_time :: Ptr () -> Ptr () -> IO CInt
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_process_stats :: ProcessPtr -> IO Error
+
+#{enum ProcessSource, ProcessSource
+    , sourceEntire = sg_entire_process_count
+    , sourceLast   = sg_last_process_count
+    }
+
+data instance Struct ProcessCount = CProcessCount
+    { countTotal    :: {-# UNPACK #-} !CULLong
+    , countRunning  :: {-# UNPACK #-} !CULLong
+    , countSleeping :: {-# UNPACK #-} !CULLong
+    , countStopped  :: {-# UNPACK #-} !CULLong
+    , countZombie   :: {-# UNPACK #-} !CULLong
+    , countUnknown  :: {-# UNPACK #-} !CULLong
+    , countSystime  :: {-# UNPACK #-} !CTime
+    }
+
+instance Copy ProcessCount where
+    copy ptr = do
+        CProcessCount{..} <- peek ptr
+        ProcessCount <%> countTotal
+                     <#> countRunning
+                     <#> countSleeping
+                     <#> countStopped
+                     <#> countZombie
+                     <#> countUnknown
+                     <@> countSystime
+
+instance Storable (Struct ProcessCount) where
+    alignment _ = #{alignment sg_process_count}
+    sizeOf    _ = #{size      sg_process_count}
+
+    peek p = CProcessCount
+        <$> #{peek sg_process_count, total} p
+        <*> #{peek sg_process_count, running} p
+        <*> #{peek sg_process_count, sleeping} p
+        <*> #{peek sg_process_count, stopped} p
+        <*> #{peek sg_process_count, zombie} p
+        <*> #{peek sg_process_count, unknown} p
+        <*> #{peek sg_process_count, systime} p
+
+    poke p CProcessCount{..} = do
+        #{poke sg_process_count, total} p countTotal
+        #{poke sg_process_count, running} p countRunning
+        #{poke sg_process_count, sleeping} p countSleeping
+        #{poke sg_process_count, stopped} p countStopped
+        #{poke sg_process_count, zombie} p countZombie
+        #{poke sg_process_count, unknown} p countUnknown
+        #{poke sg_process_count, systime} p countSystime
+
+foreign import ccall safe "statgrab.h sg_get_process_count_of"
+    sg_get_process_count_of :: ProcessSource -> IO ProcessCountPtr
+
+foreign import ccall safe "statgrab.h sg_get_process_count_r"
+    sg_get_process_count_r :: ProcessPtr -> IO ProcessCountPtr
+
+foreign import ccall safe "statgrab.h sg_free_stats_buf"
+    sg_free_process_count :: ProcessCountPtr -> IO Error
diff --git a/src/System/Statgrab/Internal.hs b/src/System/Statgrab/Internal.hs
new file mode 100644
--- /dev/null
+++ b/src/System/Statgrab/Internal.hs
@@ -0,0 +1,226 @@
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+{-# LANGUAGE TypeFamilies               #-}
+
+-- Module      : System.Statgrab.Internal
+-- Copyright   : (c) 2013 Brendan Hay <brendan.g.hay@gmail.com>
+-- License     : This Source Code Form is subject to the terms of
+--               the Mozilla Public License, v. 2.0.
+--               A copy of the MPL can be found in the LICENSE file or
+--               you can obtain it at http://mozilla.org/MPL/2.0/.
+-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
+-- Stability   : experimental
+-- Portability : non-portable (GHC extensions)
+
+-- | Interface types for copying and unmarshalling libstatgrab structs into a
+-- more programmer friendly Haskell format.
+module System.Statgrab.Internal where
+
+import Control.Applicative
+import Data.ByteString       (ByteString)
+import Data.Time.Clock.POSIX
+import Foreign
+import Foreign.C.Types
+
+type Entries = Ptr CSize
+
+newtype Error = Error CInt
+    deriving (Eq, Show)
+
+newtype HostState = HostState CInt
+    deriving (Eq, Show, Storable)
+
+data Host = Host
+    { hostOsName    :: !ByteString
+    , hostOsRelease :: !ByteString
+    , hostOsVersion :: !ByteString
+    , hostPlatform  :: !ByteString
+    , hostName      :: !ByteString
+    , hostBitWidth  :: !Integer
+    , hostState     :: !HostState
+    , hostNCPU      :: !Integer
+    , hostMaxCPU    :: !Integer
+    , hostUptime    :: !POSIXTime
+    , hostSystime   :: !POSIXTime
+    } deriving (Eq, Show)
+
+data CPU = CPU
+    { cpuUser                   :: !Integer
+    , cpuKernel                 :: !Integer
+    , cpuIdle                   :: !Integer
+    , cpuIOWait                 :: !Integer
+    , cpuSwap                   :: !Integer
+    , cpuNice                   :: !Integer
+    , cpuTotal                  :: !Integer
+    , cpuCtxSwitches            :: !Integer
+    , cpuVoluntaryCtxSwitches   :: !Integer
+    , cpuInvoluntaryCtxSwitches :: !Integer
+    , cpuSyscalls               :: !Integer
+    , cpuInterrupts             :: !Integer
+    , cpuSoftInterrupts         :: !Integer
+    , cpuSystime                :: !POSIXTime
+    } deriving (Eq, Show)
+
+newtype CPUPercentSource = CPUPercentSource CInt
+    deriving (Eq, Show, Storable)
+
+data CPUPercent = CPUPercent
+    { cpuPctUser      :: !Double
+    , cpuPctKernel    :: !Double
+    , cpuPctIdle      :: !Double
+    , cpuPctIOWait    :: !Double
+    , cpuPctSwap      :: !Double
+    , cpuPctNice      :: !Double
+    , cpuPctTimeTaken :: !POSIXTime
+    } deriving (Eq, Show)
+
+data Memory = Memory
+    { memTotal   :: !Integer
+    , memFree    :: !Integer
+    , memUsed    :: !Integer
+    , memCache   :: !Integer
+    , memSystime :: !POSIXTime
+    } deriving (Eq, Show)
+
+data Load = Load
+    { load1       :: !Double
+    , load5       :: !Double
+    , load15      :: !Double
+    , loadSystime :: !POSIXTime
+    } deriving (Eq, Show)
+
+data User = User
+    { userLoginName    :: !ByteString
+    , userRecordId     :: !ByteString
+    , userRecordIdSize :: !Integer
+    , userDevice       :: !ByteString
+    , userHostName     :: !ByteString
+    , userPid          :: !Integer
+    , userLoginTime    :: !POSIXTime
+    , userSystime      :: !POSIXTime
+    } deriving (Eq, Show)
+
+data Swap = Swap
+    { swapTotal   :: !Integer
+    , swapUsed    :: !Integer
+    , swapFree    :: !Integer
+    , swapSystime :: !POSIXTime
+    } deriving (Eq, Show)
+
+newtype DeviceType = DeviceType CInt
+    deriving (Eq, Show, Storable)
+
+data FileSystem = FileSystem
+    { fsDeviceName  :: !ByteString
+    , fsType        :: !ByteString
+    , fsMountPoint  :: !ByteString
+    , fsDeviceType  :: !DeviceType
+    , fsSize        :: !Integer
+    , fsUsed        :: !Integer
+    , fsFree        :: !Integer
+    , fsAvail       :: !Integer
+    , fsTotalInodes :: !Integer
+    , fsUsedInodes  :: !Integer
+    , fsFreeInodes  :: !Integer
+    , fsAvailInodes :: !Integer
+    , fsIOSize      :: !Integer
+    , fsBlockSize   :: !Integer
+    , fsTotalBlocks :: !Integer
+    , fsFreeBlocks  :: !Integer
+    , fsUsedBlocks  :: !Integer
+    , fsAvailBlocks :: !Integer
+    , fsSystime     :: !POSIXTime
+    } deriving (Eq, Show)
+
+data DiskIO = DiskIO
+    { diskName    :: !ByteString
+    , diskRead    :: !Integer
+    , diskWrite   :: !Integer
+    , diskSystime :: !POSIXTime
+    } deriving (Eq, Show)
+
+data NetworkIO = NetworkIO
+    { ifaceIOName     :: !ByteString
+    , ifaceTX         :: !Integer
+    , ifaceRX         :: !Integer
+    , ifaceIPackets   :: !Integer
+    , ifaceOPackets   :: !Integer
+    , ifaceIErrors    :: !Integer
+    , ifaceOErrors    :: !Integer
+    , ifaceCollisions :: !Integer
+    , ifaceSystem     :: !POSIXTime
+    } deriving (Eq, Show)
+
+newtype InterfaceMode = InterfaceMode CInt
+    deriving (Eq, Show, Storable)
+
+newtype InterfaceStatus = InterfaceStatus CInt
+    deriving (Eq, Show, Storable)
+
+data NetworkInterface = NetworkInterface
+    { ifaceName    :: !ByteString
+    , ifaceSpeed   :: !Integer
+    , ifaceFactor  :: !Integer
+    , ifaceDuplex  :: !InterfaceMode
+    , ifaceUp      :: !InterfaceStatus
+    , ifaceSystime :: !POSIXTime
+    } deriving (Eq, Show)
+
+data Page = Page
+    { pagesIn      :: !Integer
+    , pagesOut     :: !Integer
+    , pagesSysTime :: !POSIXTime
+    } deriving (Eq, Show)
+
+newtype ProcessState = ProcessState CInt
+    deriving (Eq, Show, Storable)
+
+data Process = Process
+    { procName        :: !ByteString
+    , procTitle       :: !ByteString
+    , procPid         :: !Integer
+    , procParent      :: !Integer
+    , procPGid        :: !Integer
+    , procSessId      :: !Integer
+    , procUid         :: !Integer
+    , procEUid        :: !Integer
+    , procGid         :: !Integer
+    , procEGid        :: !Integer
+    , procSwitches    :: !Integer
+    , procVoluntary   :: !Integer
+    , procInvoluntary :: !Integer
+    , procSize        :: !Integer
+    , procResident    :: !Integer
+    , procStart       :: !POSIXTime
+    , procSpent       :: !POSIXTime
+    , procCPUPercent  :: !Double
+    , procNice        :: !Integer
+    , procState       :: !ProcessState
+    , procSystime     :: !POSIXTime
+    } deriving (Eq, Show)
+
+newtype ProcessSource = ProcessSource CInt
+    deriving (Eq, Show)
+
+data ProcessCount = ProcessCount
+    { countTotal    :: !Integer
+    , countRunning  :: !Integer
+    , countSleeping :: !Integer
+    , countStopped  :: !Integer
+    , countZombie   :: !Integer
+    , countUnknown  :: !Integer
+    , countSystime  :: !POSIXTime
+    } deriving (Eq, Show)
+
+infixl 4 <%>, <#>, <@>, <!>
+
+(<%>) :: (Integral a, Applicative f) => (Integer -> b) -> a -> f b
+(<%>) a b = a <$> pure (toInteger b)
+
+(<#>) :: (Integral a, Applicative f) => f (Integer -> b) -> a -> f b
+(<#>) a b = a <*> pure (toInteger b)
+
+(<@>) :: (Fractional a, Real c, Applicative f) => f (a -> b) -> c -> f b
+(<@>) a b = a <*> pure (realToFrac b)
+
+(<!>) :: Applicative f => f (a -> b) -> a -> f b
+(<!>) a b = a <*> pure b
diff --git a/statgrab.cabal b/statgrab.cabal
new file mode 100644
--- /dev/null
+++ b/statgrab.cabal
@@ -0,0 +1,64 @@
+name:                  statgrab
+version:               0.1.0
+synopsis:              Collect system level metrics and statistics
+homepage:              http://github.com/brendanhay/statgrab
+license:               OtherLicense
+license-file:          LICENSE
+author:                Brendan Hay
+maintainer:            Brendan Hay <brendan.g.hay@gmail.com>
+copyright:             Copyright (c) 2013 Brendan Hay
+stability:             Experimental
+category:              System, FFI, Monitoring
+build-type:            Simple
+cabal-version:         >= 1.10
+
+description:
+    Provides an interface to the cross platform system statistics C library libstatgrab.
+    .
+    It supports a wide range of system statistics including CPU usage, memory utilisation,
+    disk usage, process counts, network traffic, disk I/O, and more.
+    .
+    The current list of supported and tested platforms for the underlying library
+    includes OSX, FreeBSD, Linux, NetBSD, OpenBSD, Solaris, DragonFly BSD, HP-UX and AIX.
+    .
+    /Requirements:/
+    .
+    * libstatgrab @0.9.0@ must be installed on the target system.
+    .
+    * Be aware that currently these bindings have been developed and tested
+      only on OSX/Linux.
+
+extra-source-files:
+    README.md
+
+source-repository head
+    type:     git
+    location: git://github.com/brendanhay/statgrab.git
+
+library
+    default-language:  Haskell2010
+    hs-source-dirs:    src
+
+    exposed-modules:
+        System.Statgrab
+
+    other-modules:
+        System.Statgrab.Base
+      , System.Statgrab.Internal
+
+    ghc-options:
+        -Wall -O2
+
+    build-depends:
+        async
+      , base                      >= 4 && < 5
+      , bytestring
+      , MonadCatchIO-transformers
+      , time
+      , transformers
+
+    extra-libraries:
+        statgrab
+
+    includes:
+        statgrab.h
