1
0
mirror of https://github.com/Tafkas/fritzbox-munin.git synced 2023-10-10 13:36:55 +02:00

code optimization

This commit is contained in:
Christian Stade-Schuldt 2015-06-05 00:59:24 +02:00
parent 9e8e067531
commit 2bd15e836d
7 changed files with 57 additions and 46 deletions

View File

@ -22,21 +22,21 @@ import sys
import fritzbox_helper as fh import fritzbox_helper as fh
PAGE = '/system/ecostat.lua' PAGE = '/system/ecostat.lua'
pattern = re.compile('.*\/(StatTemperature)".*=.*"(.*?)"') pattern = re.compile(".*/(StatTemperature)\".*=.*\"(.*?)\"")
def get_cpu_temperature(): def get_cpu_temperature():
"""get the current cpu temperature""" """get the current cpu temperature"""
server = os.environ['fritzbox_ip'] server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password'] password = os.environ['fritzbox_password']
sid = fh.get_sid(server, password) sid = fh.get_sid(server, password)
data = fh.get_page(server, sid, PAGE) data = fh.get_page(server, sid, PAGE)
m = re.search(pattern, data) m = re.search(pattern, data)
if m: if m:
print 'temp.value %d' %(int(m.group(2).split(',')[0])) print 'temp.value %d' % (int(m.group(2).split(',')[0]))
def print_config(): def print_config():
@ -58,7 +58,7 @@ if __name__ == '__main__':
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print 'yes' print 'yes'
elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch':
# Some docs say it'll be called with fetch, some say no arg at all # Some docs say it'll be called with fetch, some say no arg at all
try: try:
get_cpu_temperature() get_cpu_temperature()
except: except:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python ##!/usr/bin/env python
""" """
fritzbox_cpu_usage - A munin plugin for Linux to monitor AVM Fritzbox fritzbox_cpu_usage - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt Copyright (C) 2015 Christian Stade-Schuldt
@ -22,21 +22,21 @@ import sys
import fritzbox_helper as fh import fritzbox_helper as fh
PAGE = '/system/ecostat.lua' PAGE = '/system/ecostat.lua'
pattern = re.compile('.*\/(StatCPU)".*=.*"(.*?)"') pattern = re.compile(".*/(StatCPU)\".*=.*\"(.*?)\"")
def get_cpu_usage(): def get_cpu_usage():
"""get the current cpu usage""" """get the current cpu usage"""
server = os.environ['fritzbox_ip'] server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password'] password = os.environ['fritzbox_password']
sid = fh.get_sid(server, password) sid = fh.get_sid(server, password)
data = fh.get_page(server, sid, PAGE) data = fh.get_page(server, sid, PAGE)
m = re.search(pattern, data) m = re.search(pattern, data)
if m: if m:
print 'cpu.value %d' %(int(m.group(2).split(',')[0])) print 'cpu.value %d' % (int(m.group(2).split(',')[0]))
def print_config(): def print_config():
@ -58,7 +58,7 @@ if __name__ == '__main__':
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print 'yes' print 'yes'
elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch':
# Some docs say it'll be called with fetch, some say no arg at all # Some docs say it'll be called with fetch, some say no arg at all
try: try:
get_cpu_usage() get_cpu_usage()
except: except:

View File

@ -22,22 +22,22 @@ import sys
import fritzbox_helper as fh import fritzbox_helper as fh
PAGE = '/system/ecostat.lua' PAGE = '/system/ecostat.lua'
pattern = re.compile('.*\/(StatRAM.*?)".*=.*"(.*?)"') pattern = re.compile(".*/(StatRAM.*?)\".*=.*\"(.*?)\"")
USAGE_MAPPING = {'StatRAMCacheUsed': 'cache', 'StatRAMPhysFree': 'free', 'StatRAMStrictlyUsed': 'strict'} USAGE_MAPPING = {'StatRAMCacheUsed': 'cache', 'StatRAMPhysFree': 'free', 'StatRAMStrictlyUsed': 'strict'}
def get_memory_usage(): def get_memory_usage():
"""get the current memory usage""" """get the current memory usage"""
server = os.environ['fritzbox_ip'] server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password'] password = os.environ['fritzbox_password']
sid = fh.get_sid(server, password) sid = fh.get_sid(server, password)
data = fh.get_page(server, sid, PAGE) data = fh.get_page(server, sid, PAGE)
matches = re.finditer(pattern, data) matches = re.finditer(pattern, data)
if matches: if matches:
for m in matches: for m in matches:
print'%s.value %d' % (USAGE_MAPPING[m.group(1)], int(m.group(2).split(',')[0])) print'%s.value %d' % (USAGE_MAPPING[m.group(1)], int(m.group(2).split(',')[0]))
def print_config(): def print_config():
@ -65,7 +65,7 @@ if __name__ == '__main__':
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print 'yes' print 'yes'
elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch':
# Some docs say it'll be called with fetch, some say no arg at all # Some docs say it'll be called with fetch, some say no arg at all
try: try:
get_memory_usage() get_memory_usage()
except: except:

View File

@ -22,22 +22,23 @@ import sys
import fritzbox_helper as fh import fritzbox_helper as fh
PAGE = '/system/energy.lua' PAGE = '/system/energy.lua'
pattern = re.compile('.*\/(.*act?)".*=.*"(.*?)"') pattern = re.compile(".*/(.*act?)\".*=.*\"(.*?)\"")
DEVICE_MAPPING = { 'rate_abact' : 'ab', 'rate_dspact': 'dsl' , 'rate_sumact': 'system', DEVICE_MAPPING = {'rate_abact': 'ab', 'rate_dspact': 'dsl', 'rate_sumact': 'system',
'rate_systemact': 'cpu', 'rate_usbhostact': 'usb', 'rate_wlanact': 'wifi'} 'rate_systemact': 'cpu', 'rate_usbhostact': 'usb', 'rate_wlanact': 'wifi'}
def get_power_consumption(): def get_power_consumption():
"""get the current power consumption usage""" """get the current power consumption usage"""
server = os.environ['fritzbox_ip'] server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password'] password = os.environ['fritzbox_password']
sid = fh.get_sid(server, password) sid = fh.get_sid(server, password)
data = fh.get_page(server, sid, PAGE) data = fh.get_page(server, sid, PAGE)
matches = re.finditer(pattern, data) matches = re.finditer(pattern, data)
if matches: if matches:
for m in matches: for m in matches:
print'%s.value %d' % (DEVICE_MAPPING[m.group(1)], int(m.group(2))) print'%s.value %d' % (DEVICE_MAPPING[m.group(1)], int(m.group(2)))
def print_config(): def print_config():
@ -89,7 +90,7 @@ if __name__ == '__main__':
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print 'yes' print 'yes'
elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch':
# Some docs say it'll be called with fetch, some say no arg at all # Some docs say it'll be called with fetch, some say no arg at all
try: try:
get_power_consumption() get_power_consumption()
except: except:

View File

@ -14,26 +14,31 @@
#%# capabilities=autoconf #%# capabilities=autoconf
""" """
import sys, os import sys
from fritzconnection import FritzConnection from fritzconnection import FritzConnection
def print_values(): def print_values():
try: try:
connection = FritzConnection() con = FritzConnection()
except Exception as e: except Exception as e:
sys.exit("Couldn't get WAN traffic") sys.exit("Couldn't get WAN traffic")
down_traffic = connection.call_action('WANCommonInterfaceConfig', 'GetTotalBytesReceived')['NewTotalBytesReceived'] down_traffic = con.call_action('WANCommonInterfaceConfig', 'GetTotalBytesReceived')['NewTotalBytesReceived']
print ('down.value %d' % down_traffic) print ('down.value %d' % down_traffic)
up_traffic = connection.call_action('WANCommonInterfaceConfig', 'GetTotalBytesSent')['NewTotalBytesSent'] up_traffic = con.call_action('WANCommonInterfaceConfig', 'GetTotalBytesSent')['NewTotalBytesSent']
print ('up.value %d' % up_traffic) print ('up.value %d' % up_traffic)
max_down_traffic = connection.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')['NewLayer1DownstreamMaxBitRate'] max_down_traffic = con.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')[
'NewLayer1DownstreamMaxBitRate']
print ('maxdown.value %d' % max_down_traffic) print ('maxdown.value %d' % max_down_traffic)
max_up_traffic = connection.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')['NewLayer1UpstreamMaxBitRate'] max_up_traffic = con.call_action('WANCommonInterfaceConfig', 'GetCommonLinkProperties')[
print ('maxup.value %d' % max_up_traffic) 'NewLayer1UpstreamMaxBitRate']
print ('maxup.value %d' % max_up_traffic)
def print_config(): def print_config():
print "graph_title AVM Fritz!Box WAN traffic" print "graph_title AVM Fritz!Box WAN traffic"
@ -64,13 +69,14 @@ def print_config():
print "maxup.draw LINE1" print "maxup.draw LINE1"
print "maxup.info Maximum speed of the WAN interface." print "maxup.info Maximum speed of the WAN interface."
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == 'config': if len(sys.argv) == 2 and sys.argv[1] == 'config':
print_config() print_config()
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print "yes" # Some docs say it'll be called with fetch, some say no arg at all print "yes" # Some docs say it'll be called with fetch, some say no arg at all
elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'): elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'):
try: try:
print_values() print_values()
except: except:
sys.exit("Couldn't retrieve fritzbox traffic") sys.exit("Couldn't retrieve fritzbox traffic")

View File

@ -14,18 +14,21 @@
#%# capabilities=autoconf #%# capabilities=autoconf
""" """
import sys, os import sys
from fritzconnection import FritzConnection from fritzconnection import FritzConnection
def print_values(): def print_values():
try: try:
connection = FritzConnection() connection = FritzConnection()
except Exception as e: except Exception as e:
sys.exit("Couldn't get connection uptime") sys.exit("Couldn't get connection uptime")
uptime = connection.call_action('WANIPConnection', 'GetStatusInfo')['NewUptime'] uptime = connection.call_action('WANIPConnection', 'GetStatusInfo')['NewUptime']
print ('uptime.value %.2f' % (int(uptime)/86400.0)) print ('uptime.value %.2f' % (int(uptime) / 86400.0))
def print_config(): def print_config():
print "graph_title AVM Fritz!Box Connection Uptime" print "graph_title AVM Fritz!Box Connection Uptime"
print "graph_args --base 1000 -l 0" print "graph_args --base 1000 -l 0"
@ -35,13 +38,14 @@ def print_config():
print "uptime.label uptime" print "uptime.label uptime"
print "uptime.draw AREA" print "uptime.draw AREA"
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == 'config': if len(sys.argv) == 2 and sys.argv[1] == 'config':
print_config() print_config()
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print "yes" # Some docs say it'll be called with fetch, some say no arg at all print "yes" # Some docs say it'll be called with fetch, some say no arg at all
elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'): elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'):
try: try:
print_values() print_values()
except: except:
sys.exit("Couldn't retrieve fritzbox connection uptime") sys.exit("Couldn't retrieve fritzbox connection uptime")

View File

@ -22,7 +22,7 @@ import sys
import fritzbox_helper as fh import fritzbox_helper as fh
PAGE = '/system/energy.lua' PAGE = '/system/energy.lua'
pattern = re.compile('(\d+) WLAN') pattern = re.compile("(\d+) WLAN")
def get_connected_wifi_devices(): def get_connected_wifi_devices():
@ -57,7 +57,7 @@ if __name__ == '__main__':
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf': elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print 'yes' print 'yes'
elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch':
# Some docs say it'll be called with fetch, some say no arg at all # Some docs say it'll be called with fetch, some say no arg at all
try: try:
get_connected_wifi_devices() get_connected_wifi_devices()
except: except: