root / plugins / router / arris-sb6183_uptime @ 7b078749
Historique | Voir | Annoter | Télécharger (3,4 ko)
| 1 |
#!/usr/bin/env python3 |
|---|---|
| 2 |
|
| 3 |
# Copyright 2020 Nathaniel Clark <nathaniel.clark@misrule.us> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU Library General Public License as published by |
| 7 |
# the Free Software Foundation; version 2 only |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU Library General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Library General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
# |
| 18 |
|
| 19 |
""" |
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
arris-sb6183_uptime - Uptime monitoring for Arris SB6183 Cable Modem |
| 23 |
|
| 24 |
=head1 CONFIGURATION |
| 25 |
|
| 26 |
Make sure 192.168.100.1 is accessible through your firewall. |
| 27 |
|
| 28 |
To have this register with munin as it's own host set the "env.hostname" in config. |
| 29 |
Also ensure that the hostname set is listed in munin.conf. |
| 30 |
|
| 31 |
[arris*] |
| 32 |
env.hostname modem |
| 33 |
|
| 34 |
=head1 TESTING |
| 35 |
|
| 36 |
Developed and tested with: |
| 37 |
firmware: D30CM-OSPREY-2.4.0.1-GA-02-NOSH |
| 38 |
hardware version: 1 |
| 39 |
|
| 40 |
=head1 VERSION |
| 41 |
|
| 42 |
0.0.1 |
| 43 |
|
| 44 |
=head1 AUTHOR |
| 45 |
|
| 46 |
Nathaniel Clark <nathaniel.clark@misrule.us> |
| 47 |
|
| 48 |
=head1 LICENSE |
| 49 |
|
| 50 |
GPLv2 |
| 51 |
|
| 52 |
=head1 MAGIC MARKERS |
| 53 |
|
| 54 |
#%# family=contrib |
| 55 |
#%# capabilities=autoconf |
| 56 |
|
| 57 |
=cut |
| 58 |
""" |
| 59 |
import re |
| 60 |
import os |
| 61 |
import sys |
| 62 |
from urllib import request |
| 63 |
|
| 64 |
|
| 65 |
HOSTNAME = os.getenv("hostname", None)
|
| 66 |
URL = "http://192.168.100.1/RgSwInfo.asp" |
| 67 |
|
| 68 |
if len(sys.argv) == 2: |
| 69 |
if sys.argv[1] == "config": |
| 70 |
if HOSTNAME: |
| 71 |
print("host_name {0}".format(HOSTNAME))
|
| 72 |
|
| 73 |
# POWER |
| 74 |
print( |
| 75 |
"""graph_title Modem Uptime |
| 76 |
graph_category system |
| 77 |
graph_args --base 1000 -l 0 |
| 78 |
graph_vlabel uptime in days |
| 79 |
graph_scale no |
| 80 |
graph_category system |
| 81 |
graph_info This graph shows the number of days that the the host is up and running so far. |
| 82 |
uptime.label uptime |
| 83 |
uptime.info The system uptime itself in days. |
| 84 |
uptime.draw AREA |
| 85 |
""" |
| 86 |
) |
| 87 |
sys.exit(0) |
| 88 |
|
| 89 |
if sys.argv[1] == "autoconfig": |
| 90 |
try: |
| 91 |
from lxml import html |
| 92 |
|
| 93 |
resp = request.urlopen(URL) |
| 94 |
except ImportError: |
| 95 |
print("no (missing lxml module)")
|
| 96 |
except OSError: |
| 97 |
print("no (no router)")
|
| 98 |
else: |
| 99 |
if page.status_code == 200: |
| 100 |
print("yes")
|
| 101 |
else: |
| 102 |
print("no (Bad status code: %d)" % page.status_code)
|
| 103 |
sys.exit(0) |
| 104 |
|
| 105 |
from lxml import html |
| 106 |
|
| 107 |
rxblank = re.compile(r"[\x00\n\r\t ]+", re.MULTILINE) |
| 108 |
rxcomment = re.compile(r"<!--.*?-->") |
| 109 |
rxscript = re.compile(r"<script.*?</script>", re.MULTILINE) |
| 110 |
|
| 111 |
resp = request.urlopen(URL) |
| 112 |
if resp.status != 200: |
| 113 |
print( |
| 114 |
"failed to get status page %d: %s" % (resp.status, resp.reason), file=sys.stderr |
| 115 |
) |
| 116 |
print("uptime.value U")
|
| 117 |
sys.exit(0) |
| 118 |
|
| 119 |
data = rxscript.sub( |
| 120 |
"", |
| 121 |
rxcomment.sub( |
| 122 |
"", |
| 123 |
rxblank.sub(" ", "".join(map(lambda x: x.decode("utf-8"), resp.readlines()))),
|
| 124 |
), |
| 125 |
) |
| 126 |
dom = html.fromstring(data) |
| 127 |
|
| 128 |
arr = dom.xpath('//table[contains(@class, "simpleTable")]')
|
| 129 |
trs = arr[1].findall("tr")
|
| 130 |
# drop title |
| 131 |
trs.pop(0) |
| 132 |
|
| 133 |
date = "".join(trs[0].findall("td")[1].itertext()).strip()
|
| 134 |
|
| 135 |
arr = date.split(" ")
|
| 136 |
rx = re.compile(r"[hms]") |
| 137 |
days = int(arr[0]) |
| 138 |
hms = rx.sub("", arr[2]).split(":")
|
| 139 |
|
| 140 |
seconds = ((days * 24 + int(hms[0])) * 60 + int(hms[1])) * 60 + int(hms[2]) |
| 141 |
print("uptime.value {0}".format(seconds / 86400.0))
|
