mirror of
https://github.com/FreifunkFranken/fff-monitoring.git
synced 2024-06-14 03:13:57 +02:00
commit
9753dca9e3
|
@ -1,85 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import lxml.etree
|
||||
import requests
|
||||
import datetime
|
||||
from pymongo import MongoClient
|
||||
client = MongoClient()
|
||||
|
||||
db = client.freifunk
|
||||
|
||||
|
||||
CONFIG = {
|
||||
"crawl_netif": "br-mesh",
|
||||
"mac_netif": "br-mesh",
|
||||
"vpn_netif": "fffVPN"
|
||||
}
|
||||
|
||||
# create db indexes
|
||||
db.routers.create_index([("position", "2dsphere")])
|
||||
|
||||
|
||||
tree = lxml.etree.fromstring(requests.get("https://netmon.freifunk-franken.de/api/rest/routerlist", params={"limit": 5000}).content)
|
||||
|
||||
for r in tree.xpath("/netmon_response/routerlist/router"):
|
||||
user_netmon_id = int(r.xpath("user_id/text()")[0])
|
||||
user = db.users.find_one({"netmon_id": user_netmon_id})
|
||||
if user:
|
||||
user_id = user["_id"]
|
||||
else:
|
||||
user_id = db.users.insert({
|
||||
"netmon_id": user_netmon_id,
|
||||
"nickname": r.xpath("user/nickname/text()")[0]
|
||||
})
|
||||
user = db.users.find_one({"_id": user_id})
|
||||
|
||||
router = {
|
||||
"netmon_id": int(r.xpath("router_id/text()")[0]),
|
||||
"hostname": r.xpath("hostname/text()")[0],
|
||||
"user": {"nickname": user["nickname"], "_id": user["_id"]}
|
||||
}
|
||||
|
||||
try:
|
||||
lng = float(r.xpath("longitude/text()")[0])
|
||||
lat = float(r.xpath("latitude/text()")[0])
|
||||
assert lng != 0
|
||||
assert lat != 0
|
||||
|
||||
router["position"] = {
|
||||
"type": "Point",
|
||||
"coordinates": [lng, lat]
|
||||
}
|
||||
|
||||
# define hood
|
||||
router["hood"] = db.hoods.find_one({"position": {"$near": {"$geometry": router["position"]}}})["name"]
|
||||
|
||||
# try to get comment
|
||||
position_comment = r.xpath("location/text()")[0]
|
||||
if position_comment != "undefined":
|
||||
router["position"]["comment"] = position_comment
|
||||
except (IndexError, AssertionError):
|
||||
pass
|
||||
|
||||
try:
|
||||
router["description"] = r.xpath("description/text()")[0]
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
if db.routers.find_one({"netmon_id": router["netmon_id"]}):
|
||||
print("Updating »%(hostname)s«" % router)
|
||||
db.routers.update_one({"netmon_id": router["netmon_id"]}, {"$set": router})
|
||||
else:
|
||||
print("Importing »%(hostname)s«" % router)
|
||||
# crawl HTML page for CONFIG["crawl_netif"] ip for 1st direct router crawl
|
||||
page = requests.get("https://netmon.freifunk-franken.de/router.php", params={"router_id": router["netmon_id"]}).text
|
||||
if CONFIG["crawl_netif"] in page:
|
||||
netif_ip = "fe80%s" % page.split("<b>%s</b>" % CONFIG["crawl_netif"])[1].split("fe80")[1].split("/")[0]
|
||||
router["netifs"] = [{
|
||||
"name": CONFIG["crawl_netif"],
|
||||
"ipv6_fe80_addr": netif_ip
|
||||
}]
|
||||
|
||||
router["created"] = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
|
||||
router["status"] = "unknown"
|
||||
|
||||
db.routers.insert_one(router)
|
|
@ -44,20 +44,6 @@ def import_nodewatcher_xml(mac, xml):
|
|||
|
||||
router_update = parse_nodewatcher_xml(xml)
|
||||
|
||||
if not "position" in router_update:
|
||||
# pre-webui router
|
||||
if not router or not "netmon_id" in router:
|
||||
# new router
|
||||
# fetch additional information from netmon as it is not yet contained in xml
|
||||
router_info = netmon_fetch_router_info(mac)
|
||||
if router_info:
|
||||
events.append({
|
||||
"time": utcnow(),
|
||||
"type": "netmon",
|
||||
"comment": "Fetched metadata from netmon",
|
||||
})
|
||||
router_update.update(router_info)
|
||||
|
||||
# keep hood up to date
|
||||
if not "hood" in router_update:
|
||||
# router didn't send his hood in XML
|
||||
|
@ -407,59 +393,3 @@ def parse_nodewatcher_xml(xml):
|
|||
return router_update
|
||||
except (AssertionError, lxml.etree.XMLSyntaxError, IndexError) as e:
|
||||
raise ValueError("%s: %s" % (e.__class__.__name__, str(e)))
|
||||
|
||||
def netmon_fetch_router_info(mac):
|
||||
mac = mac.replace(":", "").lower()
|
||||
try:
|
||||
tree = lxml.etree.fromstring(requests.get("https://netmon.freifunk-franken.de/api/rest/router/%s" % mac).content)
|
||||
except lxml.etree.XMLSyntaxError:
|
||||
return None
|
||||
|
||||
for r in tree.xpath("/netmon_response/router"):
|
||||
user_netmon_id = int(r.xpath("user_id/text()")[0])
|
||||
user = db.users.find_one({"netmon_id": user_netmon_id})
|
||||
if not user:
|
||||
nickname = r.xpath("user/nickname/text()")[0]
|
||||
user = db.users.find_one({"nickname": nickname})
|
||||
if user:
|
||||
# non-netmon user with email key for new routers gets old router
|
||||
db.users.update_one({"_id": user['_id']}, {"$set": {"netmon_id": user_netmon_id}})
|
||||
else:
|
||||
# netmon user gets old user
|
||||
user_id = db.users.insert({
|
||||
"netmon_id": user_netmon_id,
|
||||
"nickname": nickname
|
||||
})
|
||||
user = db.users.find_one({"_id": user_id})
|
||||
|
||||
router = {
|
||||
"netmon_id": int(r.xpath("router_id/text()")[0]),
|
||||
"user": {"nickname": user["nickname"], "_id": user["_id"]}
|
||||
}
|
||||
|
||||
try:
|
||||
lng = float(r.xpath("longitude/text()")[0])
|
||||
lat = float(r.xpath("latitude/text()")[0])
|
||||
assert lng != 0
|
||||
assert lat != 0
|
||||
|
||||
router["position"] = {
|
||||
"type": "Point",
|
||||
"coordinates": [lng, lat]
|
||||
}
|
||||
|
||||
# try to get comment
|
||||
position_comment = r.xpath("location/text()")[0]
|
||||
if position_comment != "undefined" and position_comment != " ":
|
||||
router["position_comment"] = position_comment
|
||||
else:
|
||||
router["position_comment"] = ""
|
||||
except (IndexError, AssertionError):
|
||||
router["position_comment"] = ""
|
||||
|
||||
try:
|
||||
router["description"] = r.xpath("description/text()")[0]
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
return router
|
||||
|
|
|
@ -90,11 +90,7 @@
|
|||
<div class="panel-body">
|
||||
<table class="table table-condensed">
|
||||
<tr><th>Hostname</th><td>
|
||||
{%- if router.netmon_id %}
|
||||
<a href="https://netmon.freifunk-franken.de/router.php?router_id={{ router.netmon_id }}">{{ router.hostname }}</a>
|
||||
{%- else %}
|
||||
{{ router.hostname }}
|
||||
{%- endif %}
|
||||
{%- if router.netifs|webui_addr %}
|
||||
(<a href="http://[{{ router.netifs|webui_addr }}]">WebUI</a>)
|
||||
{%- endif %}
|
||||
|
|
Loading…
Reference in New Issue
Block a user