snmp 0.2.0.1 → 0.2.1.1
raw patch · 6 files changed
+85/−29 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Network.Snmp.Client: ConfigV1 :: Hostname -> Port -> Int -> Community -> Config
Files
- snmp.cabal +1/−1
- src/Network/Protocol/Snmp.hs +5/−2
- src/Network/Snmp/Client.hs +18/−13
- src/Network/Snmp/Client/Types.hs +7/−2
- src/Network/Snmp/Client/Version2.hs +32/−8
- src/Network/Snmp/Example.hs +22/−3
snmp.cabal view
@@ -1,5 +1,5 @@ name: snmp-version: 0.2.0.1+version: 0.2.1.1 synopsis: API for write snmp client. description: API for write snmp client. license: BSD3
src/Network/Protocol/Snmp.hs view
@@ -186,6 +186,8 @@ bit i = 1 `shiftL` i #endif +-- | Phantom type for version 1 (Header V2, PDU V2)+data V1 -- | Phantom type for version 2 (Header V2, PDU V2) data V2 -- | Phantom type for version 3 (Header V3, PDU V3)@@ -359,7 +361,7 @@ instance Construct (Version -> Packet) where initial Version3 = V3Packet Version3 initial initial initial Version2 = V2Packet Version2 initial initial- initial Version1 = error "not inplemented"+ initial Version1 = V2Packet Version1 initial initial instance Construct (Header V3) where initial = V3Header (ID 0) (MaxSize 65007) (Flag False NoAuthNoPriv) UserBasedSecurityModel initial@@ -662,15 +664,16 @@ _ -> throw $ ServerException 10 instance ASN1Object Packet where+ toASN1 (V2Packet Version1 header body) _ = Start Sequence : toASN1 Version1 (toASN1 header (toASN1 body [End Sequence])) toASN1 (V2Packet Version2 header body) _ = Start Sequence : toASN1 Version2 (toASN1 header (toASN1 body [End Sequence])) toASN1 (V3Packet Version3 header body) _ = Start Sequence : toASN1 Version3 (toASN1 header (toASN1 body [End Sequence])) toASN1 _ _ = throw $ ServerException 10 fromASN1 asn = flip runParseASN1State asn $ onNextContainer Sequence $ do v <- getObject case v of+ Version1 -> V2Packet Version1 <$> getObject <*> getObject Version2 -> V2Packet Version2 <$> getObject <*> getObject Version3 -> V3Packet Version3 <$> getObject <*> getObject- _ -> throw $ ServerException 10 instance ASN1Object Value where toASN1 NoSuchObject xs = Other Context 0 "" : xs
src/Network/Snmp/Client.hs view
@@ -38,16 +38,21 @@ import Network.Snmp.Client.Version3 client :: Config -> IO Client-client conf@ConfigV2{..} = clientV2 hostname - port - timeout - community-client conf@ConfigV3{..} = clientV3 hostname - port - timeout - sequrityName - authPass - privPass - sequrityLevel - authType - privType +client ConfigV1{..} = clientV1 hostname + port + timeout + community+client ConfigV2{..} = clientV2 hostname + port + timeout + community+client ConfigV3{..} = clientV3 hostname + port + timeout + sequrityName + authPass + privPass + sequrityLevel + authType + privType +
src/Network/Snmp/Client/Types.hs view
@@ -11,11 +11,16 @@ type Login = ByteString -data Config = ConfigV2+data Config = ConfigV1 { hostname :: Hostname , port :: Port , timeout :: Int , community :: Community+ } | ConfigV2+ { hostname :: Hostname+ , port :: Port+ , timeout :: Int+ , community :: Community } | ConfigV3 { hostname :: Hostname , port :: Port@@ -44,7 +49,7 @@ } instance Construct (Version -> Config) where- initial Version1 = ConfigV2 "localhost" "161" (sec 5) (Community "public") + initial Version1 = ConfigV1 "localhost" "161" (sec 5) (Community "public") initial Version2 = ConfigV2 "localhost" "161" (sec 5) (Community "public") initial Version3 = ConfigV3 "localhost" "161" (sec 5) "guest" "" "" AuthNoPriv "" MD5 DES
src/Network/Snmp/Client/Version2.hs view
@@ -1,5 +1,6 @@ module Network.Snmp.Client.Version2-( clientV2+( clientV1+, clientV2 ) where @@ -22,6 +23,9 @@ v2 :: Packet v2 = initial Version2 +v1 :: Packet+v1 = initial Version1+ returnResult2 :: NS.Socket -> Int -> IO Suite returnResult2 socket timeout = do result <- race (threadDelay timeout) (decode <$> recv socket 1500 :: IO Packet)@@ -34,17 +38,30 @@ setRCS :: Community -> OIDS -> Packet -> Packet setRCS c o = setCommunityP c . setSuite (Suite $ map (`Coupla` Zero) o) +clientV1 :: Hostname -> Port -> Int -> Community -> IO Client+clientV1 = clientV12 v1+ clientV2 :: Hostname -> Port -> Int -> Community -> IO Client-clientV2 hostname port timeout community = do+clientV2 = clientV12 v2++clientV12 :: Packet -> Hostname -> Port -> Int -> Community -> IO Client+clientV12 packet hostname port timeout community = do socket <- makeSocket hostname port uniqInteger <- uniqID ref <- newIORef uniqInteger let - req oids = setRCS community oids v2+ req oids = setRCS community oids packet get' oids = withSocketsDo $ do+ let packet' = req oids+ version = getVersion packet' rid <- succCounter ref- sendAll socket $ encode $ setRequest (GetRequest rid 0 0) (req oids) - returnResult2 socket timeout+ sendAll socket $ encode $ setRequest (GetRequest rid 0 0) packet'+ case version of+ Version2 -> returnResult2 socket timeout+ Version1 -> catch (returnResult2 socket timeout) (fixErrorV1Get oids)+ Version3 -> error "imposible"+ fixErrorV1Get oids (ServerException 2) = return $ Suite $ map (`Coupla` NoSuchObject) oids+ fixErrorV1Get _ e = throwIO e bulkget' oids = withSocketsDo $ do rid <- succCounter ref@@ -52,9 +69,16 @@ returnResult2 socket timeout getnext' oids = withSocketsDo $ do+ let packet' = req oids+ version = getVersion packet' rid <- succCounter ref- sendAll socket $ encode $ setRequest (GetNextRequest rid 0 0) (req oids)- returnResult2 socket timeout+ sendAll socket $ encode $ setRequest (GetNextRequest rid 0 0) packet'+ case version of+ Version2 -> returnResult2 socket timeout+ Version1 -> catch (returnResult2 socket timeout) (fixErrorV1GetNext oids)+ Version3 -> error "imposible"+ fixErrorV1GetNext oids (ServerException 2) = return $ Suite $ map (`Coupla` EndOfMibView) oids+ fixErrorV1GetNext _ e = throwIO e walk' oids base accumulator | oids == base = do@@ -86,7 +110,7 @@ (True, _) -> return $ accumulator <> filtered first set' oids = withSocketsDo $ do rid <- succCounter ref- sendAll socket $ encode $ setRequest (SetRequest rid 0 0) . setCommunityP community . setSuite oids $ v2+ sendAll socket $ encode $ setRequest (SetRequest rid 0 0) . setCommunityP community . setSuite oids $ packet returnResult2 socket timeout return Client
src/Network/Snmp/Example.hs view
@@ -34,7 +34,12 @@ {- $config > -- First you must create config-> -- For SNMPv3+> -- For SNMPv1+> conf1 :: Config+> conf1 = (initial Version1) { hostname = "salt" +> , community = Community "helloall"+> } +> -- For SNMPv2 > conf2 :: Config > conf2 = (initial Version2) { hostname = "salt" > , community = Community "helloall"@@ -52,6 +57,10 @@ > } > -}+conf1 :: Config+conf1 = (initial Version1) { hostname = "salt" + , community = Community "helloall"+ } conf2 :: Config conf2 = (initial Version2) { hostname = "salt" , community = Community "helloall"@@ -79,6 +88,11 @@ > client2 = bracket (client conf2) > close > requests+>+> client1 :: IO ()+> client1 = bracket (client conf1)+> close+> requests > -} client3 :: IO ()@@ -91,6 +105,11 @@ close requests +client1 :: IO ()+client1 = bracket (client conf1)+ close+ requests+ {- $oids > -- Describe oids which you need > root, eth0, tabl, ipAddr, zeroDotZero :: [Integer]@@ -115,13 +134,14 @@ ipAddr = [1,3,6,1,2,1,4,22,1,3,3,192,168,3,1] zeroDotZero = [1,3,6,1,2,1,2,2,1,22,20] -oi, sysUptime, memory, sysContact, bad, testOid :: ByteString+oi, sysUptime, memory, sysContact, bad, testOid, inet :: ByteString oi = ".1.3.6.1.2.1.1.9.1.2.1" sysUptime = "1.3.6.1.2.1.25.1.1.0" memory = "1.3.6.1.2.1.25.2" sysContact = "1.3.6.1.2.1.1.4.0" bad = "1.4.6.1.2.1.1.4" testOid = "1.3.6.1.2.1.25.1.1.0"+inet = ".1.3.6.1" {- $request > -- Describe requests@@ -149,7 +169,6 @@ requests :: Client -> IO () requests snmp = do print "get request"- putStr . show =<< get snmp [oidFromBS testOid] putStr . show =<< get snmp [oidFromBS testOid] putStr . show =<< get snmp [oidFromBS sysUptime, oidFromBS oi, zeroDotZero] print "bulkget request"