Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "easysnmp" and "snmpwalk" for huge efficiency gains #19

Merged
merged 3 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

This plugin uses ```snmpv3``` with ```MD5``` + ```AES``` to check a lot of different values on your Synology DiskStation.

This check plugin needs ```pysnmp``` to be installed on your system. You can install it with: ```pip3 install pysnmp```
This check plugin is based on ```easysnmp```, you can install it on your system with ```pip install easysnmp```.
Note that ```easysnmp``` needs ```net-snmp```, so you might also need to install ```libsnmp-dev``` and
```snmp-mibs-downloader``` on your system, like ```apt install --yes libsnmp-dev```.


Usage:
```
Expand Down Expand Up @@ -86,7 +89,7 @@ Make sure to set ```synology_snmp_user```, ```synology_snmp_autkey``` and ```syn

If you want to add a missing check or another value to be added than you can use the [official Synology MIB Guide](https://global.download.synology.com/download/Document/MIBGuide/Synology_DiskStation_MIB_Guide.pdf) as a hint for the right MIBs / OIDs and start a pull-request.

This plugin was tested successfully with DS215j and DS718+
This plugin was tested successfully with DS215j, DS216+ and DS718+.

Note: As of version 0.2 and higher only python3 is supported. Version 0.1 was the last python2 compatible release.

Expand Down
74 changes: 38 additions & 36 deletions check_synology.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pysnmp.hlapi import *
from pysnmp.proto.errind import RequestTimedOut
import argparse
import sys
import math
import re

import easysnmp

AUTHOR = "Frederic Werner"
VERSION = 0.2
amotl marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -30,31 +30,36 @@

state = 'OK'

try:
session = easysnmp.Session(
hostname=hostname,
version=3,
security_level="auth_with_privacy",
security_username=user_name,
auth_password=auth_key,
auth_protocol="MD5",
privacy_password=priv_key,
privacy_protocol="AES128")

except easysnmp.EasySNMPError as e:
print(e)
exit(-1)

def snmpget(oid):
global state
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
UsmUserData(user_name, auth_key, priv_key, authProtocol=usmHMACMD5AuthProtocol, privProtocol=usmAesCfb128Protocol),
UdpTransportTarget((hostname, port)),
ContextData(),
ObjectType(ObjectIdentity(oid)
#.addAsn1MibSource('file:///usr/share/snmp', 'http://mibs.snmplabs.com/asn1/@mib@')
))
)

if errorIndication:
print(errorIndication)
if isinstance(errorIndication, RequestTimedOut):
state = "UNKNOWN"
exitCode()
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
#answer = (' = '.join([x.prettyPrint() for x in varBind]))
#print(' = '.join([x.prettyPrint() for x in varBind]))
return varBind[-1].prettyPrint()
try:
res = session.get(oid)
return res.value
except easysnmp.EasySNMPError as e:
print(e)

# Walk the given OID and return all child OIDs as a list of tuples of OID and value
def snmpwalk(oid):
res = []
try:
res = session.walk(oid)
except easysnmp.EasySNMPError as e:
print(e)
return res

def exitCode():
if state == 'OK':
Expand Down Expand Up @@ -98,13 +103,9 @@ def exitCode():
maxDisk = 0
output = ''
perfdata = '|'
for i in range(0, 64, 1):
disk = snmpget('1.3.6.1.4.1.6574.2.1.1.2.' + str(i))
if disk.startswith("No Such Instance"):
break
maxDisk = maxDisk + 1
for i in range(0, maxDisk, 1):
disk_name = snmpget('1.3.6.1.4.1.6574.2.1.1.2.' + str(i))
for item in snmpwalk('1.3.6.1.4.1.6574.2.1.1.2'):
i = item.oid.split('.')[-1]
disk_name = item.value
disk_status_nr = snmpget('1.3.6.1.4.1.6574.2.1.1.5.' + str(i))
disk_temp = snmpget('1.3.6.1.4.1.6574.2.1.1.6.' + str(i))
status_translation = {
Expand All @@ -131,9 +132,10 @@ def exitCode():
if mode == 'storage':
output = ''
perfdata = '|'
for i in range(1,256,1):
storage_name = snmpget('1.3.6.1.2.1.25.2.3.1.3.' + str(i))
if re.match("/volume(?!.+/@docker.*)", storage_name) :
for item in snmpwalk('1.3.6.1.2.1.25.2.3.1.3'):
i = item.oid.split('.')[-1]
storage_name = item.value
if re.match("/volume(?!.+/@docker.*)", storage_name):
allocation_units = snmpget('1.3.6.1.2.1.25.2.3.1.4.' + str(i))
size = snmpget('1.3.6.1.2.1.25.2.3.1.5.' + str(i))
used = snmpget('1.3.6.1.2.1.25.2.3.1.6.' + str(i))
Expand Down