Skip to content

Instantly share code, notes, and snippets.

@mochipon
Last active December 31, 2017 05:18
Show Gist options
  • Save mochipon/d829cb0b70ccc36f86dec5941697f264 to your computer and use it in GitHub Desktop.
Save mochipon/d829cb0b70ccc36f86dec5941697f264 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# mackerel-agent.conf:
#
# [plugin.metrics.cisco]
# command = "/usr/bin/python /home/guestshell/mackerel-cisco_snmp.py 1 2 8 9 10 11 12 13 14"
import commands
from datetime import datetime
import sys
import json
import os
import pickle
target = "192.168.35.1"
snmp_opt = "-v2c -c public"
buffer_file = "/tmp/mackerel-agent/cisco"
if os.path.exists(buffer_file):
with open(buffer_file, 'rb') as fp:
buffer = pickle.load(fp)
else:
buffer = {}
def fetchsnmp(oid):
result = commands.getoutput(
"snmpwalk -Ov %s %s %s | awk '{print $2}'" %
(snmp_opt, target, oid)
)
return result
def printmetric(metric, oid, mode = 'delta'):
value = fetchsnmp(oid)
if 'Such' in value:
return
time = datetime.now().strftime('%s')
if mode == 'delta':
if metric in buffer:
old_value = buffer[metric]['value']
old_time = buffer[metric]['time']
delta_value = int(value) - int(old_value)
delta_time = int(time) - int(old_time)
delta = '%.2f' % (float(delta_value) / delta_time)
print("%s %s %s" % (metric, delta, time))
buffer[metric] = {}
buffer[metric]['value'] = value
buffer[metric]['time'] = time
else:
print("%s %s %s" % (metric, value, time))
def getmeta():
define = {
"graphs": {
"cisco.cpu": {
"label": "IOS-XE - CPU Busy Percentage",
"unit": "percentage",
"metrics": [
{
"name": "5sec",
"label": "5 Sec"
},
{
"name": "1min",
"label": "1 Min"
},
{
"name": "5min",
"label": "5 Min"
}
]
},
"cisco.memory": {
"label": "IOS-XE - Memory",
"unit": "bytes",
"metrics": [
{
"name": "used",
"label": "Used"
},
{
"name": "free",
"label": "Free"
}
]
},
"cisco.interface.bandwidthbps.#": {
"label": " IOS-XE - Interface :: Bandwidth (bps)",
"unit": "bytes/sec",
"metrics": [
{
"name": "*",
"label": "%1"
}
]
},
"cisco.interface.unicast.#": {
"label": "IOS-XE - Interface :: Unicast (pps)",
"unit": "float",
"metrics": [
{
"name": "*",
"label": "%1",
"isStacked": True
}
]
},
"cisco.interface.broadcast.#": {
"label": "IOS-XE - Interface :: Broadcast (pps)",
"unit": "float",
"metrics": [
{
"name": "*",
"label": "%1",
"isStacked": True
}
]
},
"cisco.interface.multicast.#": {
"label": "IOS-XE - Interface :: Multicast (pps)",
"unit": "float",
"metrics": [
{
"name": "*",
"label": "%1",
"isStacked": True
}
]
},
"cisco.interface.discarded.#": {
"label": "IOS-XE - Interface :: Discarded (pps)",
"unit": "float",
"metrics": [
{
"name": "*",
"label": "%1",
"isStacked": True
}
]
},
"cisco.interface.error.#": {
"label": "IOS-XE - Interface :: Error (pps)",
"unit": "float",
"metrics": [
{
"name": "*",
"label": "%1",
"isStacked": True
}
]
}
}
}
meta = "# mackerel-agent-plugin\n"
meta += json.dumps(define)
return meta
def main(argv):
printmetric("cisco.cpu.5sec", ".1.3.6.1.4.1.9.9.109.1.1.1.1.3.7", None)
printmetric("cisco.cpu.1min", ".1.3.6.1.4.1.9.9.109.1.1.1.1.4.7", None)
printmetric("cisco.cpu.5min", ".1.3.6.1.4.1.9.9.109.1.1.1.1.5.7", None)
printmetric("cisco.memory.used", ".1.3.6.1.4.1.9.9.48.1.1.1.5.1", None)
printmetric("cisco.memory.free", ".1.3.6.1.4.1.9.9.48.1.1.1.6.1", None)
for i in argv[1:]:
name = fetchsnmp(".1.3.6.1.2.1.2.2.1.2.%s" % i).replace('.', '-').replace('/', '-')
printmetric("cisco.interface.bandwidthbps.%s.in" % name, ".1.3.6.1.2.1.31.1.1.1.6.%s" % i)
printmetric("cisco.interface.bandwidthbps.%s.out" % name, ".1.3.6.1.2.1.31.1.1.1.10.%s" % i)
printmetric("cisco.interface.unicast.%s.in" % name, ".1.3.6.1.2.1.31.1.1.1.7.%s" % i)
printmetric("cisco.interface.multicast.%s.in" % name, ".1.3.6.1.2.1.31.1.1.1.8.%s" % i)
printmetric("cisco.interface.broadcast.%s.in" % name, ".1.3.6.1.2.1.31.1.1.1.9.%s" % i)
printmetric("cisco.interface.unicast.%s.out" % name, ".1.3.6.1.2.1.31.1.1.1.11.%s" % i)
printmetric("cisco.interface.multicast.%s.out" % name, ".1.3.6.1.2.1.31.1.1.1.12.%s" % i)
printmetric("cisco.interface.broadcast.%s.out" % name, ".1.3.6.1.2.1.31.1.1.1.13.%s" % i)
printmetric("cisco.interface.discarded.%s.in" % name, ".1.3.6.1.2.1.2.2.1.13.%s" % i)
printmetric("cisco.interface.discarded.%s.in-UnknownProtos" % name, ".1.3.6.1.2.1.2.2.1.15.%s" % i)
printmetric("cisco.interface.discarded.%s.out" % name, ".1.3.6.1.2.1.2.2.1.19.%s" % i)
printmetric("cisco.interface.error.%s.in" % name, ".1.3.6.1.2.1.2.2.1.14.%s" % i)
printmetric("cisco.interface.error.%s.out" % name, ".1.3.6.1.2.1.2.2.1.20.%s" % i)
if __name__ == "__main__":
if os.environ.get('MACKEREL_AGENT_PLUGIN_META', '') == '1':
print(getmeta())
sys.exit(0)
main(sys.argv)
with open(buffer_file, 'wb') as fp:
pickle.dump(buffer, fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment