Il Simple Network Management Protocol (SNMP) è un protocollo di rete senza connessione che utilizzando connessioni UDP sulle porte 161 e 162, consente di semplificare la configurazione, gestione e supervisione (monitoring) di apparati collegati in una rete. Questo blocco funzione da eseguire in task Back, gestisce il protocollo SNMP agendo come Agent.
Attivando l’FB si pone in ascolto l’agente sulla porta definita in Port, sono accettate il numero di connessioni contemporanee definite in Connections. In Community occorre definire l’indirizzo della stringa di definizione della comunità accettata dall’Agent.
Per la definizione delle variabili gestite dall’Agent è definita nella libreria una apposita struttura dati. La struttura SNMPVARIABLEDEFS permette di definire le variabili su cui l’Agent può operare. In VDefs occorre fornire l’indirizzo di allocazione della struttura ed in VNr occorre definire il numero delle variabili definite. Il FB gestisce autonomamente il comando SnmpWalk permettendo di “camminare” tra tutte le variabili definite.
Descrizione
Enable (BOOL) Comando attivazione agent.
SpyOn (BOOL) Se attivo permette di spiare il funzionamento della FB.
File (eFILEP) Terminale di I/O (Stream) ritornato dalla funzione di apertura risorsa.
Version (SNMP_VERSION) Versione pacchetto SNMP (Vedi Definizione). Attualmente è gestito solo SNMP versione 1.
Community (@STRING) Indirizzo allocazione stringa definizione Community.
Timeout (TIME) Timeout gestione comando SNMP.
VDefs (@SNMPVARIABLEDEFS) Indirizzo allocazione struttura SNMPVARIABLEDEFS.
VNr (UDINT) Numero di variabili definite.
Fault (BOOL) Attivo per un loop se errore gestione.
ECode (SNMP_ERROR) Riporta l’eventuale codice di errore ricevuto (Vedi definizione).
EIndex (UDINT) Quando ECode è diverso da zero, contiene un indice che specifica quale oggetto ha generato l’errore.
Errors (UDINT) Numero di errori.
Packets (UDINT) Numero di pacchetti SNMP ricevuti.
Busy (BOOL) FB occupato nella gestione di un comando SNMP.

SNMPVARIABLEDEFS, definizione variabile SNMP
Name | Type | Description |
---|---|---|
VType | SNMP_VT | Variable type (Vedi tabella sotto) |
Writable | BOOL | TRUE: La variabile può essere scritta |
OID | @STRING | OID definition |
Address | @BYTE | Variable address |
Length | UDINT | Variable length (Accept SIZEOF) |
SNMP_VT, definizione tipo variabile
VType | Value | Description |
---|---|---|
INTEGER | 16#02 | Integer: Must refer to a signed 32bit Integer DINT variable. (Values between -2147483648 and 2147483647). |
OSTRING | 16#04 | Octect string: Must refer to a STRING variable. Arbitrary binary or textual data, typically limited to 255 characters in length. |
NULLPTR | 16#05 | Null: Must refer to a Null pointer. |
IPADDRESS | 16#40 | IPAddress: Must refer to a STRING variable. An IP address in textual data, limited to 15 characters length. |
COUNTER32 | 16#41 | Counter32: Must refer to a unsigned 32bit Integer UDINT variable. Represents a non-negative integer which monotonically increases until it reaches a maximum value of 32bits-1 (4294967295 dec), when it wraps around and starts increasing again from zero. |
GAUGE32 | 16#42 | Gauge32: Must refer to a unsigned 32bit Integer UDINT variable. Represents a non-negative integer, which holds at the maximum or minimum value specified in the range when the actual value goes over or below the range, respectively. |
TIMETICKS | 16#43 | TimeTicks: Must refer to a unsigned 32bit Integer UDINT variable. Represents an unsigned integer which represents the time, in hundredths of a second between two epochs. |
Trigger di spy
Se SpyOn attivo è possibile utilizzare utilizzare la console di spionaggio per verificare il funzionamento della FB. Sono previsti vari livelli di triggers.
Livelli di trigger
Trigger | Descrizione |
---|---|
16#00000100 | OID: Codifica OID in frame SNMP. |
16#10000000 | Lg: Log dati di esecuzione. |
16#40000000 | Er: Errori di esecuzione. |
Esempi
Come utilizzare gli esempi.
Nell’esempio viene gestito un agente SNMP, l’agente pubblica 3 variabili. Nella immagine a lato lo screenshot del comando SnmpWalk (Download) che permette di visulizzare tutti gli oggetti pubblicati dall’agent. Per eseguire l’esempio occorre importare la libreria eLLabSNMPLib nel progetto (Articolo).
LogicLab (Ptp137, ST_SNMPAgent)
PROGRAM ST_SNMPAgent
VAR
Value : DINT := [-1350]; (* Integer variable *)
SIDx : USINT; (* Socket index *)
Fp : ARRAY[0..1] OF eFILEP; (* File pointer *)
VDefs : ARRAY[0..2] OF SNMPVARIABLEDEFS; (* Variable definitions *)
UDPServer : SysUDPServer; (* UDP server *)
SNMPAgt : SNMPAgent; (* SNMP agent *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_SNMPAgent"
// *****************************************************************************
// A simple SNMP agent that responding to manager inquiries. It publish 3 OIDs.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Program initialization
IF (SysFirstLoop) THEN
// Set UDP server parameters.
UDPServer.FilesArr:=ADR(Fp); //Files array
UDPServer.LocalAdd:=ADR('0.0.0.0'); //Local address
UDPServer.LocalPort:=161; //Local port
UDPServer.MaxConn:=TO_USINT(SIZEOF(Fp)/SIZEOF(Fp[0])); //Accepted connections
UDPServer.FlushTm:=50; //Flush time (mS)
UDPServer.LifeTm:=5; //Life time (S)
UDPServer.RxSize:=512; //Rx buffer size
UDPServer.TxSize:=512; //Tx buffer size
// Parametrize the SNMP Agent FB.
SNMPAgt.SpyOn:=TRUE; //Spy command
SNMPAgt.Community:=ADR('public'); //Community
SNMPAgt.VDefs:=ADR(VDefs); //Variable definitions
SNMPAgt.VNr:=SIZEOF(VDefs)/SIZEOF(VDefs[0]); //Variable number
SNMPAgt.Timeout:=T#5s; //Timeout
// Definition of the variables on which the Agent can operate.
VDefs[0].VType:=SNMP_VT#INTEGER; //Variable type
VDefs[0].Writable:=TRUE; //Variable can be written
VDefs[0].OID:=ADR('1.3.6.1.4.1.36955.1'); //OID definition
VDefs[0].Address:=ADR(Value); //Variable address
VDefs[0].Length:=SIZEOF(Value); //Variable length
VDefs[1].VType:=SNMP_VT#OSTRING; //Variable type
VDefs[1].Writable:=FALSE; //Variable cannot be written
VDefs[1].OID:=ADR('1.3.6.1.4.1.36955.2'); //OID definition
VDefs[1].Address:=ADR('Hello!'); //Variable address
VDefs[1].Length:=Sysstrlen(VDefs[1].Address); //Variable length
VDefs[2].VType:=SNMP_VT#IPADDRESS; //Variable type
VDefs[2].Writable:=FALSE; //Variable cannot be written
VDefs[2].OID:=ADR('1.3.6.1.4.1.36955.3'); //OID definition
VDefs[2].Address:=ADR('192.168.0.12'); //Variable address
VDefs[2].Length:=Sysstrlen(VDefs[2].Address); //Variable length
END_IF;
// -------------------------------------------------------------------------
// SNMP AGENT MANAGEMENT
// -------------------------------------------------------------------------
// Manage the SNMP Agent.
UDPServer(Enable:=TRUE); //UDP server
SNMPAgt(File:=Fp[SIDx], Enable:=TRUE); //SNMP Agent
IF NOT(SNMPAgt.Busy) THEN SIDx:=SIDx+1; IF (SIDx >= UDPServer.MaxConn) THEN SIDx:=0; END_IF; END_IF;
// [End of file]