Python3 compat and cleanup

Signed-off-by: Olivier Mehani <shtrom@ssji.net>

Show exception message on error

Signed-off-by: Olivier Mehani <shtrom@ssji.net>
(cherry picked from commit 98d41e311c3c04f9ea87fc1e93104be8ef503762)
This commit is contained in:
Olivier Mehani 2019-09-24 22:22:36 +10:00
parent 15cc928242
commit f14ecaa4b3
8 changed files with 52 additions and 52 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_connection_uptime - A munin plugin for Linux to monitor AVM Fritzbox connection uptime
Copyright (C) 2015 Christian Stade-Schuldt
@ -22,9 +22,9 @@ from fritzconnection import FritzConnection
def print_values():
try:
conn = FritzConnection(address=os.environ['fritzbox_ip'])
conn = FritzConnection(address=os.getenv('fritzbox_ip'))
except Exception as e:
sys.exit("Couldn't get connection uptime")
sys.exit(f"Couldn't get connection uptime: {e}")
uptime = conn.call_action('WANIPConnection', 'GetStatusInfo')['NewUptime']
print('uptime.value %.2f' % (int(uptime) / 86400.0))
@ -39,7 +39,7 @@ def print_config():
print("uptime.label uptime")
print("uptime.draw AREA")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == "__main__":
@ -50,5 +50,5 @@ if __name__ == "__main__":
elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'):
try:
print_values()
except:
sys.exit("Couldn't retrieve fritzbox connection uptime")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox connection uptime: {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_cpu_temperature - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt
@ -27,11 +27,11 @@ PAGE = 'ecoStat'
def get_cpu_temperature():
"""get the current cpu temperature"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -52,7 +52,7 @@ def print_config():
print("temp.min 0")
print("temp.info Fritzbox CPU temperature")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_cpu_usage - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt
@ -27,11 +27,11 @@ PAGE = 'ecoStat'
def get_cpu_usage():
"""get the current cpu usage"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -52,7 +52,7 @@ def print_config():
print("cpu.min 0")
print("cpu.info Fritzbox CPU usage")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':
@ -64,5 +64,5 @@ if __name__ == '__main__':
# Some docs say it'll be called with fetch, some say no arg at all
try:
get_cpu_usage()
except:
sys.exit("Couldn't retrieve fritzbox cpu usage")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox cpu usage: {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_memory_usage - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt
@ -28,11 +28,11 @@ USAGE = ['strict', 'cache', 'free']
def get_memory_usage():
"""get the current memory usage"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -60,7 +60,7 @@ def print_config():
print("free.type GAUGE")
print("free.draw STACK")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':
@ -72,5 +72,5 @@ if __name__ == '__main__':
# Some docs say it'll be called with fetch, some say no arg at all
try:
get_memory_usage()
except:
sys.exit("Couldn't retrieve fritzbox memory usage")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox memory usage: {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# coding=utf-8
"""
fritzbox_power_consumption - A munin plugin for Linux to monitor AVM Fritzbox
@ -30,11 +30,11 @@ DEVICES = ['system', 'cpu', 'wifi', 'dsl', 'ab', 'usb']
def get_power_consumption():
"""get the current power consumption usage"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -87,7 +87,7 @@ def print_config():
print("usb.max 100")
print("usb.info Fritzbox usb devices power consumption")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':
@ -99,5 +99,5 @@ if __name__ == '__main__':
# Some docs say it'll be called with fetch, some say no arg at all
try:
get_power_consumption()
except:
sys.exit("Couldn't retrieve fritzbox power consumption")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox power consumption: {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_traffic - A munin plugin for Linux to monitor AVM Fritzbox WAN traffic
Copyright (C) 2015 Christian Stade-Schuldt
@ -22,9 +22,9 @@ from fritzconnection import FritzConnection
def print_values():
try:
conn = FritzConnection(address=os.environ['fritzbox_ip'])
conn = FritzConnection(address=os.getenv('fritzbox_ip'))
except Exception as e:
sys.exit("Couldn't get WAN traffic")
sys.exit(f"Couldn't get WAN traffic: {e}")
down_traffic = conn.call_action('WANCommonInterfaceConfig', 'GetTotalBytesReceived')['NewTotalBytesReceived']
print('down.value %d' % down_traffic)
@ -72,7 +72,7 @@ def print_config():
print("maxup.draw LINE1")
print("maxup.info Maximum speed of the WAN interface.")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == "__main__":
@ -83,5 +83,5 @@ if __name__ == "__main__":
elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'):
try:
print_values()
except:
sys.exit("Couldn't retrieve fritzbox traffic")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox traffic: {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_uptime - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt
@ -37,11 +37,11 @@ pattern = re.compile(patternLoc[locale])
def get_uptime():
"""get the current uptime"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -72,7 +72,7 @@ def print_config():
print("uptime.label uptime")
print("uptime.draw AREA")
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':
@ -84,5 +84,5 @@ if __name__ == '__main__':
# Some docs say it'll be called with fetch, some say no arg at all
try:
get_uptime()
except:
sys.exit("Couldn't retrieve fritzbox uptime")
except Exception as e:
sys.exit(f"Couldn't retrieve fritzbox uptime; {e}")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
fritzbox_wifi_devices - A munin plugin for Linux to monitor AVM Fritzbox
Copyright (C) 2015 Christian Stade-Schuldt
@ -34,11 +34,11 @@ pattern = re.compile(patternLoc[locale])
def get_connected_wifi_devices():
"""gets the numbrer of currently connected wifi devices"""
server = os.environ['fritzbox_ip']
password = os.environ['FRITZ_PASSWORD']
server = os.getenv('fritzbox_ip')
password = os.getenv('FRITZ_PASSWORD')
if "FRITZ_USERNAME" in os.environ:
fritzuser = os.environ['FRITZ_USERNAME']
fritzuser = os.getenv('FRITZ_USERNAME')
session_id = fh.get_session_id(server, password, fritzuser)
else:
session_id = fh.get_session_id(server, password)
@ -61,7 +61,7 @@ def print_config():
print('wifi.graph LINE1')
print('wifi.info Wifi Connections on 2.4 & 5 Ghz')
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
print("host_name " + os.getenv('host_name'))
if __name__ == '__main__':
@ -73,5 +73,5 @@ if __name__ == '__main__':
# Some docs say it'll be called with fetch, some say no arg at all
try:
get_connected_wifi_devices()
except:
sys.exit("Couldn't retrieve connected fritzbox wifi devices")
except Exception as e:
sys.exit(f"Couldn't retrieve connected fritzbox wifi devices: {e}")