Vous êtes sur la page 1sur 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMP Dmysti
David Delavennat
CNRS / Centre de Gntique Molculaire

ANGD Mathrice, Novembre 2009 Rev : 106

Mathrice 2009 - CIRM

1 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan

Rappels SNMP Programmation Mtrologie dimpression Topologie rseau

Mathrice 2009 - CIRM

2 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan
1

Rappels IANA / ICANN OID ASN.1 BER SNMP Programmation Mtrologie dimpression Topologie rseau
Mathrice 2009 - CIRM 3 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

IANA / ICANN
Internet Assigned Numbers Authority : 1990 organisation internationale but non lucratif alloue lespace des adresses de protocole Internet (IP) attribue les identicateurs de protocole (UDP/TCP) gre le systme de nom de domaine de premier niveau pour les codes gnriques (gTLD) et les codes nationaux (ccTLD) assure les fonctions de gestion du systme de serveurs racines

Internet Corporation for Assigned Names and Numbers : 1998 organisation de droit priv but non lucratf alloue lespace des adresses de protocole Internet (IP) attribue les identicateurs de protocole (UDP/TCP) gre le systme de nom de domaine de premier niveau pour les codes gnriques (gTLD) et les codes nationaux (ccTLD) assure les fonctions de gestion du systme de serveurs racines LICANN assume prsent les fonctions de lIANA

Mathrice 2009 - CIRM

4 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Object IDentier
identiants universels de ressources reprsents sous la forme dune suite dentiers organiss sous forme hirarchique assurent linteroprabilit entre diffrents logiciels seul lorganisme 1.2.3 peut dire quelle est la signication de lOID 1.2.3.4 ont t dnis dans une recommandation de lInternational Telecommunication Union. LIETF a propos de reprsenter la suite dentiers constituant les OID spars par des points. Il est possible dobtenir un OID, et par consquence toute une branche, auprs de lIANA. OID 0 1 2 2.5 2.5.4 2.5.6 1.3.6.1 1.3.6.1.4 1.3.6.1.4.1.4203 Description Branche ITU Branche ISO Branche commune entre lITU et lISO Service X500 Dnition des types dattributs Dnition des classes dobjets Internet OID IANA-assigned company OIDs (private MIBs) OpenLDAP

Mathrice 2009 - CIRM

5 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Abstract Syntax Notation .1


Le langage ASN.1 spcie un format dencodage dinformations au sein dun ux de donnes ainsi que des types de donnes. celui-ci dcrit des syntaxes abstraites (totalement indpendante des syntaxes de transferts) le typage des donnes est explicite (le type transmis est transfr sur le rseau) les dnitions des types sont dans des modules (rutilisables ensuite avec importation) un type est dni par une collection de valeurs qui peut tre distingue effectivement dautres valeurs partir de types existants, on peut dnir des sous-types, ce qui permet ainsi la dnition de types de faon rcursive Les types sont diviss en 4 groupes : les types basiques : valeurs caractrises facilement , indpendamment dautres types les types structurs : types complexes dnis partir dautre types les modules : constituent des paquetages de plusieurs dnitions de types les tags : permettent de rajouter des informations pour certains types an de faciliter le codage et dcodage La dnition dun type est constitue de 3 lments : [Nom type] ::= [Description du type] un nom qui reprsentera le nouveau type le symbole ::= la description du type Attribution dune valeur Exemple : [Nom valeur] [Nom type] ::= [Description valeur] Mathrice 2009 - CIRM 6 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Abstract Syntax Notation .1


Types de base INTEGER Domaine de valeur entier pouvant tre positif, ngatif ou nul. Rappel : Il ny a pas de limite pour les valeurs possibles car on dnit une syntaxe abstraite. On peut associer chaque valeur un identiant qui lorsquil sera employ correspondra la valeur entire dnie par lidentiant. -231 231 -1 permet dnumrer un ensemble de valeurs en associant un entier un identiant dnition dune chaine de bits de taille >= 0 squence ordonne de 0 65,535 octets type ne prenant quune seule valeur qui est null. Ce type est utilis par exemple lorsquon lon doit forcment spcier un type sans forcment transmettre dinformation TRUE et FALSE Actif : := BOOLEAN Exemple Hexadecimal ::= INTEGER { 1(1), 2(2), 3(3), 4(4), 5(5), 6(6), 7(7), 8(8), 9(9), A(10), B(11), C(12), D(13), E(14), F(15) }

ENUMERATED

Ex : Orientation : := ENUMERATED nord(0), sud(1), est(2), ouest(3) Mot32 ::= BIT STRING (SIZE(32)) ; Extension : := OCTET STRING(SIZE(3))

BIT STRING OCTET STRING NULL

BOOLEAN OID

Mathrice 2009 - CIRM

7 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Abstract Syntax Notation .1

Types complexes SEQUENCE

Domaine de valeur collection ordonne dlments distincts, les lments pouvant tre de types diffrents. Les lments peuvent tre de type OPTIONAL indiquant que la valeur nest pas ncessairement prsente dans la squence. Le mot clef DEFAULT indique que lon attribue une valeur par dfaut au composant lorsquil nest pas spci collection ordonne dlments de mme type collection non ordonne dlments non distincts qui peuvent tre de types diffrents collection non ordonne dlments non distincts du mme type collection de types, chacun tant distinct des autres correspond en fait tous les types possibles

Exemple

SEQUENCE OF SET

SET OF CHOICE ANY

Mathrice 2009 - CIRM

8 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Basic Encoding Rules

Utilis pour transmettre des donnes entre des systmes dont lencodage natif diffre Type Longueur (en nombre doctets) Donne aussi appell encodage Type-Lenght-Value Bit No. 8 0 0 1 1 _ _ 7 0 1 0 1 _ _ 6 _ _ _ _ 0 1 5 . . . . . . 4 . . . . . . 3 . . . . . . 2 . . . . . . 1 . . . . . . Implication Universal Application Context Private Primitive Data-type Constructed

Mathrice 2009 - CIRM

9 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Basic Encoding Rules : Data Type


Types INTEGER BIT STRING OCTET STRING NULL OBJECT IDENTIFIER SEQUENCE IPADDRESS COUNTER, COUNTER32 GAUGE, GAUGE32 TIMETICKS OPAQUE NSAPADDRESS COUNTER64 UINTEGER32 getRequest getNextRequest setRequest trap getBulkRequest informRequest Data-Type Primitif Primitif Primitif Primitif Primitif Constructed SNMP Application SNMP Application SNMP Application SNMP Application SNMP Application SNMP Application SNMP Application SNMP Application SNMP Context Constructed SNMP Context Constructed SNMP Context Constructed SNMP Context Constructed SNMP Context Constructed SNMP Context Constructed Hex ID 0x02 0x03 0x04 0x05 0x06 0x30 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0xA0 0xA1 0xA2 0xA4 0xA5 0xA6 Bin ID 0000 0010 0000 0011 0000 0100 0000 0101 0000 0110 0011 0000 0100 0000 0100 0001 0100 0010 0100 0011 0100 0100 0100 0101 0100 0110 0100 0111 1010 0000 1010 0001 1010 0010 1010 0100 1010 0101 1010 0110

Mathrice 2009 - CIRM

10 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan
1

Rappels SNMP MIB / SMI SNMPv1 SNMPv2 SNMPv3 Security Model VACM TRAP / INFORM Programmation Mtrologie dimpression
Mathrice 2009 - CIRM 11 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Management Information Base / Structure of Management Information

Dans quel but ? contient un identiant textuel pour chaque OID un gestionnaire SNMP utilise ces identiants pour convertir les OID en texte humainement comprhensible sans MIB, un message ne contient que des chiffres dnus de sens

Comment ? un gestionnaire SNMP importe une MIB SMI (spcication au format ASN.1) puis la compile

une compilation convertit la MIB depuis un format ASCII vers un format utilisable nativement par le gestionnaire SNMP

Mathrice 2009 - CIRM

12 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Simple Network Management Protocol

Mathrice 2009 - CIRM

13 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv1 / SNMPSec

SNMPv1

RFC1155 RFC1156 RFC1157 RFC1213

Mai 1990 Mai 1990 Mai 1990 Mars 1991

Structure and identication of management information for TCP/IP-based internets Management Information Base for network management of TCP/IP-based internets Simple Network Management Protocol (SNMP) Management Information Base for Network Management of TCP/IP-based internets : MIB-II

SNMPSec

RFC1351 RFC1352 RFC1353

Juillet 1992 Juillet 1992 Juillet 1992

SNMP Administrative Model SNMP Security Protocols Denitions of Managed Objects for Administration of SNMP Parties

Mathrice 2009 - CIRM

14 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv1

Mathrice 2009 - CIRM

15 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv2
SNMPv2p : party-based mise jour des oprations du protocole nouvelles oprations et nouveaux types de donnes scurit base sur les groupes de SNMPsec RFC1441 RFC1442 RFC1443 RFC1444 RFC1445 RFC1446 RFC1447 RFC1448 RFC1449 RFC1450 RFC1451 RFC1452 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Avril 1993 Introduction to version 2 of the Internet-standard Network Management Framework Structure of Management Information for SNMPv2 Textual Conventions for SNMPv2 Conformance Statements for SNMPv2 Administrative Model for SNMPv2 Security Protocols for SNMPv2 Party MIB for SNMPv2 Protocol Operations for SNMPv2 Transport Mappings for SNMPv2 Management Information Base for SNMPv2 Manager-to-Manager Management Information Base Coexistence between version 1 and version 2 of the Internet-standard Network Management Framework Mathrice 2009 - CIRM 16 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv2

SNMPv2c : community-based aussi appel " community stringbased SNMPv2 " amlioration des oprations de protocole et des types doprations de SNMPv2p utilise la scurit par chaine de caractres "community" de SNMPv1 RFC1901 RFC1902 RFC1903 RFC1904 RFC1905 RFC1906 RFC1907 RFC1908 Janvier 1996 Janvier 1996 Janvier 1996 Janvier 1996 Janvier 1996 Janvier 1996 Janvier 1996 Janvier 1996 Introduction to Community-based SNMPv2 Structure of Management Information for SNMPv2 Textual Conventions for SNMPv2 Conformance Statements for SNMPv2 Protocol Operations for SNMPv2 Transport Mappings for SNMPv2 Management Information Base for SNMPv2 Coexistence between SNMPv1 and SNMPv2

Mathrice 2009 - CIRM

17 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv2

SNMPv2u : user-based utilise les oprations, les types de donnes de SNMPv2c scurit base sur les usagers RFC1009 RFC1010 SNMPv2* combine les meilleures parties de SNMPv2p et SNMPv2u. documents dcrivant cette version jamais publis Fvrier 1996 Fvrier 1996 An Administrative Infrastructure for SNMPv2 User-based Security Model for SNMPv2

Mathrice 2009 - CIRM

18 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv3

scurit base sur les usagers (SNMPv2u et SNMPv2*) types et oprations de SNMPv2p

Mathrice 2009 - CIRM

19 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv3
RFC2576 RFC2578 RFC2579 RFC2580 RFC3410 RFC3411 RFC3412 RFC3413 RFC3414 RFC3415 RFC3416 RFC3417 RFC3418 March 2000 Avril 1999 April 1999 April 1999 December 2002 December 2002 December 2002 December 2002 December 2002 December 2002 December 2002 December 2002 December 2002 Coexistence between Version 1, Version 2, and Version 3 of the Internetstandard Network Management Framework Structure of Management Information Version 2 (SMIv2) Textual Conventions for SMIv2 Conformance Statements for SMIv2 Introduction and Applicability Statements for Internet-Standard Management Framework An Architecture for Describing Simple Network Management Protocol (SNMP) Management Frameworks Message Processing and Dispatching for the Simple Network Management Protocol (SNMP) Simple Network Management Protocol (SNMP) Applications User-based Security Model (USM) for version 3 of the Simple Network Management Protocol (SNMPv3) View-based Access Control Model (VACM) for the Simple Network Management Protocol (SNMP) Version 2 of the Protocol Operations for the Simple Network Management Protocol (SNMP) Transport Mappings for the Simple Network Management Protocol (SNMP) Management Information Base (MIB) for the Simple Network Management Protocol (SNMP) Mathrice 2009 - CIRM

20 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

SNMPv3

Mathrice 2009 - CIRM

21 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Security Model : CSM

Community-based Security Model (SNMPv1 / SNMPv2c) nom de communaut, en clair public private

1 2 3 4 5 6 7 8 9 10 11 12

# # NET SNMP # rocommunity rwcommunity rocommunity6 rwcommunity6 rocommunity rwcommunity rocommunity6 rwcommunity6

COMMUNITY COMMUNITY COMMUNITY COMMUNITY COMMUNITY COMMUNITY COMMUNITY COMMUNITY

[SOURCE [SOURCE [SOURCE [SOURCE

[ OID ] ] [ OID ] ] [ OID ] ] [ OID ] ] VIEW VIEW VIEW VIEW

SOURCE V SOURCE V SOURCE V SOURCE V

Mathrice 2009 - CIRM

22 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Security Model : USM

User-based Security Model (SNMPv3) authentication : empche quelquun de changer le paquet en cours de route cryptage : empche quiconque de lire les informations de gestion contenues dans un paquet estampillage du temps : empche la rutilisation dun paquet

1 2 3 4 5 6 7 8

# # NET SNMP # r o u s e r USER [ noauth | auth | p r i v [ OID ] ] rwuser USER [ noauth | auth | p r i v [ OID ] ] r o u s e r USER noauth | auth | p r i v V NAME rwuser USER noauth | auth | p r i v V NAME

Mathrice 2009 - CIRM

23 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Security Model : USM - auth

Mathrice 2009 - CIRM

24 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Security Model : USM - priv

Mathrice 2009 - CIRM

25 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

View-based Access Control Model : Algorithme

Mathrice 2009 - CIRM

26 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

View-based Access Control Model : Exemple


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # h t t p : / / www. netsnmp . org / w i k i / i n d e x . php / Vacm # F i r s t , map t h e community name (COMMUNITY) i n t o a s e c u r i t y name # ( l o c a l and mynetwork , depending on where t h e r e q u e s t i s coming from ) : # sec . name source community com2sec l o c a l localhost secret42 com2sec custom_sec 1 9 2 . 1 6 8 . 1 . 0 / 2 4 p u b l i c # Second , map t h e s e c u r i t y names i n t o group names : # sec . model sec . name group custom_grp v1 custom_sec group custom_grp v2c custom_sec group i n c r e m e n t a l usm myuser # SNMPv3 username == sec . name # T h i r d , c r e a t e a view f o r us t o l e t t h e groups have r i g h t s t o : # i n c l / excl subtree mask view a l l included .1 view custom_v excluded . 1 view custom_v i n c l u d e d sysUpTime . 0 view custom_v i n c l u d e d i n t e r f a c e s . i f T a b l e view m i n i _ v i e w excluded . 1 80 view m i n i _ v i e w i n c l u d e d sysUpTime . 0 view i f _ v i e w excluded . 1 80 view i f _ v i e w i n c l u d e d sysUpTime . 0 view i f _ v i e w included ifTable # F i n a l l y , g r a n t t h e groups access t o t h e i r views : # c o n t e x t sec . model sec . l e v e l match read write notif access MyRWGroup "" any noauth exact a l l all none access custom_grp " " any noauth e x a c t cust1_v none none access i n c r e m e n t a l " " usm noauth e x a c t m i n i _ v i e w none none access i n c r e m e n t a l " " usm auth exact i f _ v i e w none none access i n c r e m e n t a l " " usm priv e x a c t a l l _ v i e w none none Mathrice 2009 - CIRM 27 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

TRAP / INFORM

Description port UDP 162 requte SNMP non acquit (TRAP) / acquit (INFORM, uniquement SNMPv2c et SNMPv3) transporte une notication SNMP

Systme Ractif un vnement survient sur un noeud lagent associ met une requte SNMP notiant lvnement un gestionnaire SNMP

Mathrice 2009 - CIRM

28 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan
1

Rappels SNMP Programmation Exemples getRequest setRequest walk Mtrologie dimpression Topologie rseau
Mathrice 2009 - CIRM 29 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Bibliothques

Perl : http ://search.cpan.org/perldoc ?Net : :SNMP Python : http ://pysnmp.sourceforge.net/ Ruby : http ://snmplib.rubyforge.org/ PHP : php5-snmp

Mathrice 2009 - CIRM

30 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Exemples
getRequest
# . / getRequest sysDescr . 0 sysName . 0 sysDescr .0= FreeBSD davids e r v e r 7.2RELEASE p3 FreeBSD 7.2RELEASE p3 # 0 : Thu Aug 27 2 0: 4 7 : 5 2 UTC 2009 root@angd . m a t h r i c e . f r : / u s r / o b j / u s r / s r c / sys /ANGD amd64 sysName.0= davids e r v e r #

setRequest
# . / setRequest 1 . 3 . 6 . 1 . 2 . 1 . 1 . 5 . 0 snmp t e s t s e r v e r snmp b e f o r e = davids e r v e r s e t t i n g 1 . 3 . 6 . 1 . 2 . 1 . 1 . 5 . 0 t o snmp t e s t s e r v e r snmp a f t e r = snmp t e s t s e r v e r #

walk
# . / walk i f D e s c r STRING : bge0 STRING : bge1 STRING : l o 0 # Mathrice 2009 - CIRM 31 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

getRequest
Ruby
1 2 3 4 5 6 7 8 9 10 # ! / u s r / b i n / env ruby r e q u i r e rubygems r e q u i r e snmp SNMP : : Manager . open ( : Host=>localhost ) { | manager | ARGV. each { | varbind_name | manager . g e t ( varbind_name ) . each_varbind { | v a r b i n d | p r i n t "#{varbind_name}=#{varbind.value.asn1_type}: #{varbind.value}\n" } } }

PHP
1 2 3 4 5 6 7 8 9 # ! / u s r / b i n / env php <?php $ v a r b i n d s =$argv ; array_shift ( $varbinds ) ; foreach ( $ v a r b i n d s as $varbind_name ) { $ v a r b i n d _ v a l u e =snmpget ( localhost , public , $varbind_name ) ; p r i n t "{$varbind_name}={$varbind_value}\n" ; } ?> Mathrice 2009 - CIRM 32 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

setRequest
Ruby
1 2 3 4 5 6 7 8 9 10 11 # ! / u s r / b i n / env ruby r e q u i r e rubygems r e q u i r e snmp SNMP : : Manager . open ( : Host => localhost , : Community=>private ) { | manager | p u t s "snmp before=#{manager.get_value(ARGV[0])}" p u t s "setting #{ARGV[0]} to #{ARGV[1]}" v a r b i n d = SNMP : : VarBind . new (ARGV[ 0 ] , SNMP : : O c t e t S t r i n g . new (ARGV [ 1 ] ) ) manager . s e t ( v a r b i n d ) p u t s "snmp after=#{manager.get_value(ARGV[0])}" }

PHP
1 2 3 4 5 6 7 8 9 10 11 12 # ! / u s r / b i n / env php <?php a r r a y _ s h i f t ( $argv ) ; $ o b j e c t _ o i d =$argv [ 0 ] ; $ o b j e c t _ v a l u e =$argv [ 1 ] ; $ b e f o r e =snmpget ( localhost , public , $ o b j e c t _ o i d ) ; p r i n t ( "snmp before=$before\n" ) ; p r i n t ( "setting $object_oid to $object_value\n" ) ; snmpset ( localhost , private , $ o b j e c t _ o i d , s , $ o b j e c t _ v a l u e ) ; $ a f t e r =snmpget ( localhost , public , $ o b j e c t _ o i d ) ; p r i n t ( "snmp after=$after\n" ) ; ?> Mathrice 2009 - CIRM 33 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

walk
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 # ! / u s r / b i n / env ruby r e q u i r e rubygems r e q u i r e snmp SNMP : : Manager . open ( : Host=>localhost ) { | manager | ARGV. each { | o b j e c t _ o i d | manager . walk ( o b j e c t _ o i d ) { | r e s p o n s e _ o b j e c t s | r e s p o n s e _ o b j e c t s . each { | r e s p o n s e _ o b j e c t | p r i n t "#{response_object.value.asn1_type}: #{response_object.value}\n" ; } } } }

PHP
1 2 3 4 5 6 7 8 9 10 11 # ! / u s r / b i n / env php <?php $ o b j e c t s _ o i d =$argv ; array_shift ( $objects_oid ) ; foreach ( $ o b j e c t s _ o i d as $ o b j e c t _ o i d ) { $ r e s pon se _ob je cts =snmpwalk ( localhost , public , $ o b j e c t _ o i d ) ; foreach ( $r esp on se_ ob jec ts as $r es pon se _o bje ct ) { p r i n t "$response_object\n" ; } } ?> Mathrice 2009 - CIRM 34 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan
1

Rappels SNMP Programmation Mtrologie dimpression Principe PRINTER-MIB Rsultat attendu Code Topologie rseau
Mathrice 2009 - CIRM 35 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Principe

Mathrice 2009 - CIRM

36 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

PRINTER-MIB

Printer Status Normal Busy / Temporarily Unavailable Non Critical Alert Active Critical Alert Active

hrDeviceStatus running(2) running(2) warning(3)

hrPrinterStatus idle(3) printing(4) idle(3) or printing(4)

hrPrinterDetectedErrorState none set

could be : lowPaper, lowToner, or serviceRequested could be : jammed, noPaper, noToner, coverOpen, or serviceRequested

down(5)

other(1)

Unavailable Moving off-line Off-line Moving on-line Standby

down(5) warning(3) down(5) down(5) running(2)

other(1) idle(3) or printing(4) other(1) warmup(5) other(1) ofine ofine

Mathrice 2009 - CIRM

37 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Rsultat attendu : accounting.2009.3.log

Date [04/Mar /2009 [04/Mar /2009 [05/Mar /2009 [05/Mar /2009 [06/Mar /2009 : : : : : 17 18 14 18 09 : : : : : 40 10 30 08 46 : : : : : 08 + 0100] 21 + 0100] 16 + 0100] 25 + 0100] 45 + 0100]

Printer p26black p26color p26color p26black p26black

User babar mafalda mafalda mafalda haddoc

Page count 1 8 1 1 10

Mathrice 2009 - CIRM

38 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : accounting.rb

1 2 3 4

r e q u i r e cups/backend backend=CUPS : : Backend : : Accounting . new backend . p r i n t

Mathrice 2009 - CIRM

39 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_backend_print.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 module CUPS module Backend class Accounting def p r i n t p r e _ p r i n t _ p a g e _ c o u n t = s e l f . p r i n t e r . page_count backend_return_code = s e l f . p r i n t e r . p r i n t ( ) p r i n t i n g _ i n _ p r o g r e s s = true while p r i n t i n g _ i n _ p r o g r e s s self . p r i n t e r . get_status i f ( s e l f . p r i n t e r . i s _ i d l e ? ) then s e l f . p r i n t e r . s t a t u s _ s t a b i l i t y =1 i f ( s e l f . p r i n t e r . s t a t u s _ i d l e _ i s _ s t a b l e ? ) then printing_in_progress = false end else self . printer . s t a t u s _ s t a b i l i t y = 5 end sleep 1 end p o s t _ p r i n t _ p a g e _ c o u n t = s e l f . p r i n t e r . page_count s e l f . p r i n t e r . printed_page_count = post_print_page_count pre_print_page_count s e l f . account e x i t backend_return_code end end end end Mathrice 2009 - CIRM 40 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_backend.rb

1 2 3 4 5 6 7 8 9 10 11 12 13

r e q u i r e cups/printer module CUPS module Backend class Accounting def i n i t i a l i z e @log_dir = . . . @ p r i n t e r = P r i n t e r . new ( . . . ) end attr_reader : printer , : log_dir end end end

Mathrice 2009 - CIRM

41 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_printer.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 r e q u i r e cups/snmp module CUPS class P r i n t e r def i n i t i a l i z e ( h = { } ) @snmp = CUPS : : Snmp . new @status_code = { : by_name => { : other => 1 , : unknown => 2 , : idle => 3 , : p r i n t i n g => 4 , : warmup => 5 } , : by_code => { 1 => other , 2 => unknown , 3 => idle , 4 => printing , 5 => warmup } } @status = @status_code [ : o t h e r ] @uri = h [: uri ] @accounting = h [ : a c c o u n t i n g ] @printed_page_count = 0 @status_stability = 5 @job = JOB . new end attr_reader : status_code , : s t a t u s , : u r i , : snmp , : job , : a c c o u n t i n g a t t r _ a c c e s s o r : printed_page_count , : s t a t u s _ s t a b i l i t y end end

Mathrice 2009 - CIRM

42 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_printer_misc.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 module CUPS class P r i n t e r def page_count SNMP : : Manager . open ( : Host=> s e l f . u r i . host , : V e r s i o n => s e l f . snmp . v e r s i o n , : Community=> s e l f . snmp . community ) { | manager | manager . g e t _ v a l u e ( s e l f . snmp . o i d [ : page_count ] ) . t o _ i } end def g e t _ s t a t u s SNMP : : Manager . open ( : Host=> s e l f . u r i . host , : V e r s i o n => s e l f . snmp . v e r s i o n , : Community=> s e l f . snmp . community ) { | manager | @status=manager . g e t _ v a l u e ( s e l f . snmp . o i d [ : p r i n t e r _ s t a t u s ] ) . t o _ i } end def i s _ p r i n t i n g ? s e l f . s t a t u s == s e l f . st a tu s _c o de [ : by_name ] [ : p r i n t i n g ] end def i s _ i d l e ? s e l f . s t a t u s == s e l f . st a tu s _c o de [ : by_name ] [ : i d l e ] end def s t a t u s _ i d l e _ i s _ s t a b l e ? s e l f . s t a t u s _ s t a b i l i t y == 0 end end end

Mathrice 2009 - CIRM

43 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_snmp_job.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

module CUPS class Snmp def i n i t i a l i z e @version = : SNMPv1 @community = public @oid = { # c f h t t p : / / www. o i d v i e w . com / mibs / 0 / P r i n t e r MIB . h t m l : page_count => 1.3.6.1.2.1.43.10.2.1.4.1.1 , : p r i n t e r _ s t a t u s => 1.3.6.1.2.1.25.3.5.1.1.1 } end a t t r _ r e a d e r : v e r s i o n , : community , : o i d end class JOB a t t r _ a c c e s s o r : j i d , : f i l e , : user_name , : t i t l e , : copies , : o p t i o n s , : f i l e _ n a m e end end

Mathrice 2009 - CIRM

44 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_uri.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

module CUPS class URI def i n i t i a l i z e ( u r i , backend ) @backend , @host , @port = n i l , n i l , n i l i f Regexp . new ( "^#{backend}://([^:]+)://([^:]+):?([0-9]*)$" ) . match ( u r i ) . n i l ? then STDOUT. p u t s "URI format error:" STDOUT. p u t s " expected accounting://<cups_uri>" STDOUT. p u t s " got #{uri}" else @backend = $1 @host = $2 @port = $3 @uri = % Q{ # { @backend } : / / # { @host } # { ( @port . n i l ? ) ? " " : " : # { @port } " ; } } end end a t t r _ r e a d e r : backend , : host , : p o r t , : u r i end end

Mathrice 2009 - CIRM

45 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cups_backend_accounting_account.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 module CUPS module Backend class Accounting def account "#time}" =~/\S+\s\S+\s\S+\s\S+\s(\S+)\s\S+/ time_zone=$1 t i m e _ s t r i n g = t i m e . s t r f t i m e ( "%d/%b/%Y:%X" ) accounting_filename =[ "#{self.log_dir}/" , "#{self.backend_name}." , "#{time.year}." , "#{time.strftime(%m)}." , "log" ]. join accounting_line =[ "[#{time_string} #{time_zone}]" , "#{self.printer.uri.host}" , "#{self.printer.job.user_name}" , "#{self.printer.printed_page_count}" ] . join ( ) F i l e . open ( a c c o u n t i n g _ f i l e n a m e , F i l e : : WRONLY| F i l e : : APPEND| F i l e : : CREAT) { | accounting_file | a c c o u n t i n g _ f i l e . puts accounting_line } end end end end Mathrice 2009 - CIRM 46 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Plan
1

Rappels SNMP Programmation Mtrologie dimpression Topologie rseau Principe CISCO-CDP-MIB Rsultat attendu Code
Mathrice 2009 - CIRM 47 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Principe

Mathrice 2009 - CIRM

48 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

CISCO-CDP-MIB.mib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ciscoCdpMIB MODULE IDENTITY : : = { ciscoMgmt 23 }

ciscoCdpMIBObjects OBJECT IDENTIFIER : : = { ciscoCdpMIB 1 } cdpInterface cdpCache cdpGlobal OBJECT IDENTIFIER : : = { ciscoCdpMIBObjects 1 } OBJECT IDENTIFIER : : = { ciscoCdpMIBObjects 2 } OBJECT IDENTIFIER : : = { ciscoCdpMIBObjects 3 }

c d p I n t e r f a c e T a b l e OBJECT TYPE SYNTAX SEQUENCE OF C d p I n t e r f a c e E n t r y MAX ACCESS nota c c e s s i b l e STATUS current DESCRIPTION " The ( c o n c e p t u a l ) t a b l e c o n t a i n i n g t h e s t a t u s o f CDP on t h e device s i n t e r f a c e s . " ::= { cdpInterface 1 } c d p I n t e r f a c e E n t r y OBJECT TYPE SYNTAX CdpInterfaceEntry MAX ACCESS nota c c e s s i b l e STATUS current DESCRIPTION " An e n t r y ( c o n c e p t u a l row ) i n t h e c d p I n t e r f a c e T a b l e , c o n t a i n i n g t h e s t a t u s o f CDP on an i n t e r f a c e . " INDEX { cdpInterfaceIfIndex } : : = { cdpInterfaceTable 1 }

Mathrice 2009 - CIRM

49 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

CISCO-CDP-MIB.yaml

1 2 3 4 5 6 7 8 9 10 11 12

ciscoCdpMIB : ciscoCdpMIBObjects : cdpCache : cdpCacheTable : cdpCacheEntry : cdpCacheAddress : cdpCacheVersion : cdpCacheDeviceId : cdpCacheDevicePort : cdpCachePlateform : cdpCachePhyLocation :

1.3.6.1.4.1.9.9.23 1.3.6.1.4.1.9.9.23.1 1.3.6.1.4.1.9.9.23.1.2 1.3.6.1.4.1.9.9.23.1.2.1 1.3.6.1.4.1.9.9.23.1.2.1.1 1.3.6.1.4.1.9.9.23.1.2.1.1.4 1.3.6.1.4.1.9.9.23.1.2.1.1.5 1.3.6.1.4.1.9.9.23.1.2.1.1.6 1.3.6.1.4.1.9.9.23.1.2.1.1.7 1.3.6.1.4.1.9.9.23.1.2.1.1.8 1.3.6.1.4.1.9.9.23.1.2.1.1.23

Mathrice 2009 - CIRM

50 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Rsultat attendu

name C3550 C2950-26-A-4 C2950-26-SM-1 C3548-26-A-1 C3548-26-A-2 C3548-26-B-1 C2950-26-B-3 C3548-26-B-2 C2950-26-B-4 C2950-26-A-3 C2960G-26-SM-2

ip 172.16.0.40 172.16.0.3 172.16.0.41 172.16.0.17 172.16.0.18 172.16.0.12 172.16.0.8 172.16.0.16 172.16.0.4 172.16.0.7 172.16.0.42

contact s.info@ s.info@ s.info@ s.info@ s.info@ s.info@ s.info@ s.info@ s.info@ s.info@ s.info@

location Bat 26 salle machine Bat 26 armoire A Bat 26 salle machine Bat 26 armoire A Bat 26 armoire A Bat 26 armoire B Bat 26 armoire B Bat 26 armoire B Bat 26 armoire B Bat 26 armoire A Bat 26 salle machine

description ...12.2(50)SE3... ...12.1(22)EA11... ...12.1(22)EA9... ...12.0(5)WC17... ...12.0(5)WC17... ...12.0(5)WC17... ...12.1(22)EA11... ...12.0(5)WC17... ...12.1(22)EA11... ...12.1(22)EA11... ...12.2(40)SE...

Mathrice 2009 - CIRM

51 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cdp_node_snmpget.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

r e q u i r e pp r e q u i r e snmp class Node def snmpget ( host , o i d ) value= n i l begin SNMP : : Manager . open ( : Host=>host , : V e r s i o n = >:SNMPv1, : Community=>public ) { | manager | v a l u e =manager . g e t _ v a l u e ( o i d ) } rescue E x c e p ti o n => _ ex c ep t io n _ pp _ e xc e pt i on _ end return value end end

Mathrice 2009 - CIRM

52 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cdp_node.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 r e q u i r e cdp_node_snmpget class Node def i n i t i a l i z e ( h = { } ) @ip = h [ : i p ] end attr_accessor : ip def sysName ( ) r e t u r n s e l f . snmpget ( i p , sysName.0 ) end def sysDescr ( ) r e t u r n s e l f . snmpget ( i p , sysDescr.0 ) end def s y s L o c a t i o n ( ) r e t u r n s e l f . snmpget ( i p , sysLocation.0 ) end def sysUptime ( ) r e t u r n s e l f . snmpget ( i p , sysUpTime.0 ) end def sysContact ( ) r e t u r n s e l f . snmpget ( i p , sysContact.0 ) end def commit @@db. execute ( INSERT INTO devices(name,ip,description,contact,location, uptime,last_update) VALUES (?,?,?,?,?,?,?) , sysName , i p , sysDescr , sysContact , s y s L o c a t i o n , sysUptime , Time . now ) end end

Mathrice 2009 - CIRM

53 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cdp_topology_discover_loop.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 r e q u i r e pp r e q u i r e snmp class Topology def d i s c o v e r _ l o o p ( root_node ) begin n e i g h bo r s = { } SNMP : : Manager . open ( : Host=>root_node . i p , : V e r s i o n = >:SNMPv1 , : Community=> public , : MibModules = >[ "CISCO-CDP-MIB" ] ) { | manager | manager . walk ( cdpCacheAddress ) { | r e s u l t | n e i g h b o r _ i p =hexa_to_decimal ( r e s u l t . v a l u e ) n e i g h b o r = nodes [ n e i g h b o r _ i p ] i f n e i g h b o r . n i l ? then n e i g h b o r = Node . new ( : i p => n e i g h b o r _ i p ) nodes [ n e i g h b o r _ i p ] = n e i g h b o r end n e i g hb o r s [ n e i g h b o r _ i p ] = n e i g h b o r } } n o d e s _ a l r e a d y _ t r e a t e d . push root_node . i p n e i g h bo r s . each { | _ , n e i g h b o r | begin n e i g h b o r . commit discover_loop ( neighbor ) end unless n o d e s _ a l r e a d y _ t r e a t e d . i n c l u d e ? n e i g h b o r . i p } rescue E x c e p ti o n => _ ex c ep t io n _ pp _ e xc e pt i on _ end end end Mathrice 2009 - CIRM 54 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cdp_topology.rb

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

r e q u i r e cdp_node r e q u i r e cdp_topology_discover_loop class Topology def i n i t i a l i z e ( h = { } ) @root = Node . new ( : i p =>h [ : r o o t _ n o d e _ i p ] ) @root . commit @nodes_already_treated = [ ] @nodes = {} end a t t r _ r e a d e r : r o o t , : nodes , : n o d e s _ a l r e a d y _ t r e a t e d def hexa_to_decimal ( h e x a _ s t r i n g ) r e t u r n h e x a _ s t r i n g . unpack ( "C*" ) . map { | n | "%d" % n } . j o i n ( . ) end def d i s c o v e r discover_loop ( root ) end end

Mathrice 2009 - CIRM

55 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Code : cdp.rb

1 2 3 4 5 6 7

r e q u i r e sqlite3 r e q u i r e cdp_topology @@db = SQLite3 : : Database . new ( topology.sqlite ) @@db. execute ( DELETE FROM devices ) t o p o l o g y =Topology . new ( : r o o t _ n o d e _ i p =>157.136.84.40 ) topology . discover

Mathrice 2009 - CIRM

56 / 57

Rappels

SNMP

Programmation

Mtrologie dimpression

Topologie rseau

Merci

Mathrice 2009 - CIRM

57 / 57

Vous aimerez peut-être aussi