Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data MIB
- data Value :: *
- data PVal
- = Read { }
- | ReadWrite {
- readAIO :: IO Value
- commitSetAIO :: Value -> IO CommitError
- testSetAIO :: Value -> IO TestError
- undoSetAIO :: Value -> IO UndoError
- rwValue :: IO Value -> (Value -> IO CommitError) -> (Value -> IO TestError) -> (Value -> IO UndoError) -> PVal
- rsValue :: Value -> PVal
- rdValue :: IO Value -> PVal
- newtype Update = Update {}
- mkObject :: Integer -> Parent -> Name -> Maybe Update -> MIB
- mkObjectType :: Integer -> Parent -> Name -> Maybe Context -> PVal -> MIB
- data CommitError
- data TestError
- data UndoError
- data Context
- agent :: FilePath -> OID -> Maybe Client -> [MIB] -> IO ()
- runAgent :: OID -> [MIB] -> Maybe Client -> Socket -> IO ()
- data Client
About
Library for write extensible SNMP agents.
data Value :: *
Snmp data types
Wrapper for value
Read | |
ReadWrite | |
|
rwValue :: IO Value -> (Value -> IO CommitError) -> (Value -> IO TestError) -> (Value -> IO UndoError) -> PVal Source
PVal constructor for read write value
Update, for rebuild oid tree in runtime
:: Integer | OID number for this object |
-> Parent | parent name for this object |
-> Name | name for this object |
-> Maybe Update | Just Update if you need dynamic module |
-> MIB | created MIB |
Constructor for MIB, create Object in mib tree
:: Integer | OID number for this object |
-> Parent | parent |
-> Name | name |
-> Maybe Context | context |
-> PVal | value |
-> MIB | created MIB |
Constructor for MIB, create Object-Type in mib tree
data CommitError Source
result for commitSetAIO (rfc 2741, section 7.2.4.2)
result for testSetAIO (rfc 2741, section 7.2.4.1)
result for undoSetAIO (rfc 2741, section 7.2.4.3)
rfc 2571 section 3.3.1, rfc 2741 section 6.1.1 Context
start agent
runAgent :: OID -> [MIB] -> Maybe Client -> Socket -> IO () Source
start agent with socket exit when catch sigQUIT, sigTERM, keyboardSignal show MIB tree when catch sigUSR1
Usage
Imports
module Main where import Network.Protocol.Snmp.AgentX import qualified Data.ByteString as BS import Data.ByteString (ByteString) import Data.Fixed (div') import Data.Time.Clock.POSIX (getPOSIXTime) import Control.Applicative ((<$>)) import Control.Monad.State import Data.IORef
Desribe values for monitoring
str :: ByteString -> PVal str x = rsValue (String x) now :: PVal now = rdValue $ TimeTicks . flip div' 1 <$> liftIO getPOSIXTime rws :: IORef Value -> PVal rws io = rwValue readV commit test undo where test (String x) | BS.length x < 10 = return NoTestError | otherwise = return TooBig test _ = return WrongType commit v = do writeIORef io v return NoCommitError undo _ = return NoUndoError readV = readIORef io rwi :: IORef Value -> PVal rwi io = rwValue readV commit test undo where test (Integer x) | x < 5 = return NoTestError | otherwise = return TooBig test _ = return WrongType commit v = do writeIORef io v return NoCommitError undo _ = return NoUndoError readV = readIORef io
Build dynamic tree
dynTree :: IORef Value -> Update dynTree i = Update $ do Integer count <- liftIO $ readIORef i return $ concatMap fun [0 .. count] where fun x = [ mkObject (fromIntegral x) "trees" ("tree" ++ show x) Nothing , mkObjectType 0 ("tree" ++ show x) "abr" Nothing (str "abr") , mkObjectType 1 ("tree" ++ show x) "abrvalg" Nothing (str "abrvalg") ]
Describe context
ver :: Maybe Context ver = Just "version"
Construct full tree
simpleTree :: IORef Value -> IORef Value -> [MIB] simpleTree m i = [ mkObject 0 "Fixmon" "about" Nothing , mkObjectType 0 "about" "name" Nothing $ rsValue (String "Fixmon agent") , mkObjectType 1 "about" "version" Nothing $ rsValue (String "0.0.1") , mkObjectType 1 "about" "version" ver $ rsValue (String "Alpha") , mkObjectType 2 "about" "comment" Nothing (rws m) , mkObject 1 "Fixmon" "dyn" Nothing , mkObjectType 0 "dyn" "counter" Nothing (rwi i) , mkObject 1 "dyn" "trees" (Just $ dynTree i) ] tree :: IO [MIB] tree = do m <- newIORef (String "init") i <- newIORef (Integer 0) return $ simpleTree m i
Start subagent
main :: IO () main = agent "/var/agentx/master" [1,3,6,1,4,1,44729] Nothing =<< tree
Examples
SNMP server config > cat /etc/snmp/snmpd.conf > rwuser sha > createUser sha SHA "password" DESi > master agentx > agentXPerms 777 775 SNMP client config > cat /etc/snmp/snmp.conf > defVersion 3 > defSecurityName sha > defSecurityLevel authPriv > defPassphrase password > defAuthType SHA > defPrivType DES > defContext "" Build example > cabal install -f example Start SNMP server > sudo snmpd Start SNMP agent > .cabal-sandbox/bin/agentx_example Get MIB tree > snmpwalk -r 1 -On localhost 1.3.6.1.4.1.44729 > .1.3.6.1.4.1.44729.0.0 = STRING: "Fixmon agent" > .1.3.6.1.4.1.44729.0.1 = STRING: "0.0.1" > .1.3.6.1.4.1.44729.0.2 = STRING: "init" > .1.3.6.1.4.1.44729.1.0 = INTEGER: 0 > .1.3.6.1.4.1.44729.1.1.0.0 = STRING: "abr" > .1.3.6.1.4.1.44729.1.1.0.1 = STRING: "abrvalg" Change MIB tree > snmpset -r 1 -On localhost .1.3.6.1.4.1.44729.1.0 i 2 > .1.3.6.1.4.1.44729.1.0 = INTEGER: 2 > snmpwalk -r 1 -On localhost 1.3.6.1.4.1.44729 > .1.3.6.1.4.1.44729.0.0 = STRING: "Fixmon agent" > .1.3.6.1.4.1.44729.0.1 = STRING: "0.0.1" > .1.3.6.1.4.1.44729.0.2 = STRING: "init" > .1.3.6.1.4.1.44729.1.0 = INTEGER: 2 > .1.3.6.1.4.1.44729.1.1.0.0 = STRING: "abr" > .1.3.6.1.4.1.44729.1.1.0.1 = STRING: "abrvalg" > .1.3.6.1.4.1.44729.1.1.1.0 = STRING: "abr" > .1.3.6.1.4.1.44729.1.1.1.1 = STRING: "abrvalg" > .1.3.6.1.4.1.44729.1.1.2.0 = STRING: "abr" > .1.3.6.1.4.1.44729.1.1.2.1 = STRING: "abrvalg"