first commit

This commit is contained in:
Pedro de Oliveira 2017-10-22 14:43:48 +01:00
commit a601f0f687
7 changed files with 567 additions and 0 deletions

84
ajaxmonit.py Executable file
View File

@ -0,0 +1,84 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import re
from datetime import datetime
import time
import requests
import mysql.connector
payload = {
'active_page': 'page_login',
'md5_pass': '',
'auth_key': '',
'username': 'home',
}
r = requests.get('http://192.168.1.1/')
cookies = r.cookies
matches = re.search(r"<INPUT type=HIDDEN name=\"auth_key\" value=\"(.+?)\">", r.text)
auth_key = matches[1]
m = hashlib.md5()
m.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8'))
hexdigest = m.hexdigest()
payload['auth_key'] = auth_key
payload['md5_pass'] = hexdigest
r = requests.post('http://192.168.1.1/index.cgi', data=payload, cookies=cookies)
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='monit')
cursor = cnx.cursor()
payload = {
'cable_settings': 1,
'active_page': 'page_cable_settings',
}
fields = {
'download': ['snr_val_dn', 'power_val_dn', 'freq_val_dn'],
'upload': ['power_val_up', 'freq_val_up', 'cannel_id_val_up'],
}
add_ajax = ("INSERT INTO ajax "
"(ts, direction, channel, frequency, power, snr) "
"VALUES (%s, %s, %s, %s, %s, %s)")
while True:
ts = datetime.now()
r = requests.post('http://192.168.1.1/index.cgi?rand=' + str(int(ts.strftime("%s"))), data=payload, cookies=cookies)
rts = datetime.strptime(r.headers['Date'], '%a, %d %b %Y %H:%M:%S GMT')
uts = rts.strftime('%Y-%m-%d %H:%M:%S')
for x in range(0, 8):
channel = {}
for f in fields['download']:
matches = re.search(r"change_field_if_exist\('(" + f + ")_(" + str(x) + ")', '(.+?)'\);", r.text)
if matches:
channel[f] = matches.group(3).replace(' dBmV', '').replace(' dB', '').replace(' MHz', '')
try:
print(uts, 0, x, channel['freq_val_dn'], channel['power_val_dn'], channel['snr_val_dn'])
cursor.execute(add_ajax, (uts, 0, x, channel['freq_val_dn'], channel['power_val_dn'], channel['snr_val_dn']))
except (mysql.connector.IntegrityError, KeyError) as err:
pass
for x in range(0, 2):
channel = {}
for f in fields['upload']:
matches = re.search(r"change_field_if_exist\('(" + f + ")_(" + str(x) + ")', '(.+?)'\);", r.text)
if matches:
channel[f] = matches.group(3).replace(' dBmV', '').replace(' MHz', '')
try:
print(uts, 1, channel['cannel_id_val_up'], channel['freq_val_up'], channel['power_val_up'])
cursor.execute(add_ajax, (uts, 1, channel['cannel_id_val_up'], channel['freq_val_up'], channel['power_val_up'], None))
except (mysql.connector.IntegrityError, KeyError) as err:
pass
cnx.commit()
time.sleep(30)

105
monit.py Executable file
View File

@ -0,0 +1,105 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import re
from datetime import datetime
import requests
from bs4 import BeautifulSoup
#from tabulate import tabulate
import mysql.connector
payload = {
'active_page': 'page_login',
'prev_page': '',
'page_title': 'Login',
'intercept_id': '-2',
'no_dns': '0',
'mimic_button_field': 'submit_button_login_submit%3A+..',
'button_value': '',
'strip_page_top': '0',
'scroll_top': '0',
'post_id': '0',
'page_title_text': 'Login',
'page_icon_number': '30',
'defval_lang': '1',
'defval_username': '',
'md5_pass': '53c8a95730e66905d86b03ae7eec26b0',
'auth_key': '1216289424',
'lang=': '1',
'username': 'home',
#'password_1636230399':''
}
r = requests.get('http://192.168.1.1/')
cookies = r.cookies
matches = re.search(r"<INPUT type=HIDDEN name=\"auth_key\" value=\"(.+?)\">", r.text)
auth_key = matches[1]
m = hashlib.md5()
m.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8'))
hexdigest = m.hexdigest()
payload['auth_key'] = auth_key
payload['md5_pass'] = hexdigest
r = requests.post('http://192.168.1.1/index.cgi', data=payload, cookies=cookies)
#ts = time.time()
ts = datetime.now()
r = requests.get('http://192.168.1.1/index.cgi?active_page=page_equipment_status', cookies=cookies)
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.find_all('table')
downstream = tables[60]
upstream = tables[61]
#print(" --- Canais Downstream ---")
downtable = []
keys = ['rx_id_val_dn_', 'status_val_dn_', 'freq_val_dn_', 'power_val_dn_', 'snr_val_dn_']
for x in range(0, 8):
row = []
for key in keys:
row.append(downstream.find("td", {"id": key + str(x)}).renderContents().strip()
.decode('utf-8').replace(' MHz', '').replace(' dBmV', '').replace(' dB', '')
)
downtable.append(row)
#headers = ['ID Recetor', 'Estado', 'Frequência (MHz)', 'Potência (dBmV)', 'SNR (dB)']
#print(tabulate(downtable, headers=headers))
#print("\n --- Canais Upstream ---")
uptable = []
keys = ['rx_id_val_dn_', 'cannel_id_val_dn_', 'freq_val_dn_', 'power_val_dn_']
for x in range(0, 2):
row = []
for key in keys:
row.append(upstream.find("td", {"id": key + str(x)}).renderContents().strip()
.decode('utf-8').replace(' MHz', '').replace(' dBmV', '')
)
uptable.append(row)
#headers = ['ID Recetor', 'Channel ID', 'Frequência (MHz)', 'Potência (dBmV)']
#print(tabulate(uptable, headers=headers, tablefmt="psql"))
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='monit')
cursor = cnx.cursor()
add_downstream = ("INSERT INTO downstream "
"(ts, id_recetor, estado, frequencia, potencia, snr) "
"VALUES (%s, %s, %s, %s, %s, %s)")
add_upstream = ("INSERT INTO upstream "
"(ts, id_recetor, channel_id, frequencia, potencia) "
"VALUES (%s, %s, %s, %s, %s)")
for line in downtable:
cursor.execute(add_downstream, (ts, line[0], line[1], line[2], line[3], line[4]))
for line in uptable:
cursor.execute(add_upstream, (ts, line[0], line[1], line[2], line[3]))
cnx.commit()
cursor.close()
cnx.close()

30
templates/graph.html Normal file
View File

@ -0,0 +1,30 @@
<html>
<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<meta http-equiv="refresh" content="300"/>
</head>
<body>
<h1>Downstream</h1>
<div id="down-potencia"></div>
<div id="down-snr"></div>
<h1>Uptream</h1>
<div id="up-potencia"></div>
<script>
Plotly.d3.json('/downstream/power', function(err, fig) {
Plotly.plot('down-potencia', fig.data, fig.layout);
});
Plotly.d3.json('/downstream/snr', function(err, fig) {
Plotly.plot('down-snr', fig.data, fig.layout);
});
Plotly.d3.json('/upstream/power', function(err, fig) {
Plotly.plot('up-potencia', fig.data, fig.layout);
});
</script>
</body>
</html>

104
templates/smooth.html Normal file
View File

@ -0,0 +1,104 @@
<html>
<head>
<!-- Plotly.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/smoothie/1.32.0/smoothie.min.js"></script>
</head>
<body onload="createTimeline()">
<h1>Downstream</h1>
<h4>Channel 1</h4>
<canvas id="channel0_0" width="500" height="100"></canvas>
<canvas id="channel0_1" width="500" height="100"></canvas>
<h4>Channel 2</h4>
<canvas id="channel1_0" width="500" height="100"></canvas>
<canvas id="channel1_1" width="500" height="100"></canvas>
<h4>Channel 3</h4>
<canvas id="channel2_0" width="500" height="100"></canvas>
<canvas id="channel2_1" width="500" height="100"></canvas>
<h4>Channel 4</h4>
<canvas id="channel3_0" width="500" height="100"></canvas>
<canvas id="channel3_1" width="500" height="100"></canvas>
<h4>Channel 5</h4>
<canvas id="channel4_0" width="500" height="100"></canvas>
<canvas id="channel4_1" width="500" height="100"></canvas>
<h4>Channel 6</h4>
<canvas id="channel5_0" width="500" height="100"></canvas>
<canvas id="channel5_1" width="500" height="100"></canvas>
<h4>Channel 7</h4>
<canvas id="channel6_0" width="500" height="100"></canvas>
<canvas id="channel6_1" width="500" height="100"></canvas>
<h4>Channel 8</h4>
<canvas id="channel7_0" width="500" height="100"></canvas>
<canvas id="channel7_1" width="500" height="100"></canvas>
<script>
var data = {'data': [] , 'chart': []}
function createTimeline() {
for(i = 0; i <= 7; i++) {
data['data'].push([new TimeSeries(), new TimeSeries()]);
}
for(i = 0; i <= 7; i++) {
var chart1 = new SmoothieChart({millisPerPixel:100});
chart1.addTimeSeries(data['data'][i][0], {lineWidth:2,strokeStyle:'#00ff00'});
chart1.streamTo(document.getElementById("channel" + i + "_0"), 2000);
data['chart'].push(chart1);
var chart2 = new SmoothieChart({millisPerPixel:100});
chart2.addTimeSeries(data['data'][i][1], {lineWidth:2,strokeStyle:'#ff0000'});
chart2.streamTo(document.getElementById("channel" + i + "_1"), 2000);
data['chart'].push(chart2);
}
}
ws = new WebSocket("ws://192.168.1.4:5678/down")
var request_data_interval
/*
ws.onopen = function()
{
request_data_interval = window.setInterval(requestData, 50);
};
*/
ws.onmessage = function (evt)
{
if ('data' in evt) {
var received_msg = evt.data;
d = JSON.parse(received_msg);
for(i = 0; i <= 7; i++) {
data['data'][i][0].append(new Date().getTime(), d['channels'][i]['power_val_dn']);
data['data'][i][1].append(new Date().getTime(), d['channels'][i]['snr_val_dn']);
}
}
};
ws.onclose = function()
{
// websocket is closed.
window.clearInterval(request_data_interval)
};
/*
function requestData()
{
//ws.send("get-data");
}
*/
</script>
</body>
</html>

82
templates/wsgraph.html Normal file
View File

@ -0,0 +1,82 @@
<html>
<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<h1>Downstream</h1>
<div id="down-potencia"></div>
<script>
first = true;
mydata = [];
ws = new WebSocket("ws://192.168.1.4:5678/down")
var request_data_interval
ws.onopen = function()
{
// Web Socket is connected, send data using send()
//ws.send("Message to send");
request_data_interval = window.setInterval(requestData, 50);
};
ws.onmessage = function (evt)
{
if ('data' in evt) {
var received_msg = evt.data;
d = JSON.parse(received_msg);
ts = d['ts'];
var arrayLength = d['channels'].length;
for (var i = 0; i < arrayLength; i++) {
if (first) {
var my_plot = {
uid: 'recetor' + i,
name: 'Canal ' + i,
x: [ts],
y: [d['channels'][i]['power_val_dn']],
type: 'lines',
};
mydata.push(my_plot);
} else {
for(z in mydata) {
//console.log(z);
if (mydata[z].uid =='recetor' + i) {
mydata[z].x.push(ts);
mydata[z].y.push(d['channels'][i]['power_val_dn']);
//console.log(z);
}
}
}
}
if (first) {
Plotly.newPlot('down-potencia', mydata);
} else {
Plotly.animate('down-potencia', mydata, { transition: { duration: 500, easing: 'cubic-in-out' } });
}
first = false;
}
};
ws.onclose = function()
{
// websocket is closed.
window.clearInterval(request_data_interval)
};
function requestData()
{
//ws.send("get-data");
}
</script>
</body>
</html>

90
web.py Normal file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask
from flask import render_template, jsonify
import mysql.connector
import copy
app = Flask(__name__)
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='monit')
@app.route('/')
def graph():
return render_template('graph.html')
@app.route('/ws')
def wsgraph():
return render_template('wsgraph.html')
@app.route('/smooth')
def smooth():
return render_template('smooth.html')
@app.route('/downstream/<name>')
def downstream(name=None):
valid = ['power', 'snr']
if name not in valid:
return 'LOL JEWS'
if name == 'power':
title = 'Power (dBmV)'
elif name == 'snr':
title = 'SNR (dB)'
output = {'data': [], 'layout': {'title': title}}
template = {'x': [], 'y': [], 'type': 'lines', 'name': '', 'uid': ''}
cursor = cnx.cursor()
for x in range(0, 8):
query = ("select ts, " + name + " from ajax where channel=%s and direction = 0 order by ts desc")
cursor.execute(query, (x,))
recetor = copy.deepcopy(template)
recetor['uid'] = 'recetor'+str(x+1)
recetor['name'] = 'Canal '+str(x+1)
for (ts, item) in cursor:
recetor['x'].append(ts.strftime('%Y-%m-%d %H:%M:%S'))
recetor['y'].append(item)
output['data'].append(recetor)
cursor.close()
cnx.commit()
return jsonify(output)
@app.route('/upstream/<name>')
def upstream(name=None):
valid = ['power']
if name not in valid:
return 'LOL JEWS'
if name == 'power':
title = 'Power (dBmV)'
output = {'data': [], 'layout': {'title': title}}
template = {'x': [], 'y': [], 'type': 'lines', 'name': '', 'uid': '', 'line': {'smoothing': 0.85}}
cursor = cnx.cursor()
for x in range(9, 11):
query = ("select ts, " + name + " from ajax where channel=%s and direction = 1 order by ts desc")
cursor.execute(query, (x,))
recetor = copy.deepcopy(template)
recetor['uid'] = 'recetor'+str(x)
recetor['name'] = 'Canal '+str(x)
for (ts, item) in cursor:
recetor['x'].append(ts.strftime('%Y-%m-%d %H:%M:%S'))
recetor['y'].append(item)
output['data'].append(recetor)
cursor.close()
cnx.commit()
return jsonify(output)

72
websockets.py Executable file
View File

@ -0,0 +1,72 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import re
from datetime import datetime
import time
import asyncio
import json
import requests
import websockets
payload = {
'active_page': 'page_login',
'md5_pass': '',
'auth_key': '',
'username': 'home',
}
r = requests.get('http://192.168.1.1/')
cookies = r.cookies
matches = re.search(r"<INPUT type=HIDDEN name=\"auth_key\" value=\"(.+?)\">", r.text)
auth_key = matches[1]
m = hashlib.md5()
m.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8'))
hexdigest = m.hexdigest()
payload['auth_key'] = auth_key
payload['md5_pass'] = hexdigest
r = requests.post('http://192.168.1.1/index.cgi', data=payload, cookies=cookies)
payload = {
'cable_settings': 1,
'active_page': 'page_cable_settings',
}
#fields = ['snr_val_dn', 'power_val_dn', 'power_val_up']
fields = ['snr_val_dn', 'power_val_dn']
async def down(websocket, path):
while True:
ts = datetime.now()
result = {'ts': ts.strftime('%Y-%m-%d %H:%M:%S'), 'channels': []}
r = requests.post('http://192.168.1.1/index.cgi?rand=' + str(int(ts.strftime("%s"))), data=payload, cookies=cookies)
matches = re.finditer(r"change_field_if_exist\('(.+?)_(\d)', '(.+?)'\);", r.text)
for m in matches:
if m.group(1) in fields:
try:
curr = result['channels'][int(m.group(2))]
except IndexError:
curr = {}
curr[m.group(1)] = float(m.group(3).replace(' dBmV', '').replace(' dB', ''))
try:
result['channels'][int(m.group(2))] = curr
except IndexError:
result['channels'].append(curr)
await websocket.send(json.dumps(result))
await asyncio.sleep(1)
start_server = websockets.serve(down, '192.168.1.4', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()