Skip to content

Commit

Permalink
Delete check_data_plan job
Browse files Browse the repository at this point in the history
  • Loading branch information
sdunesme committed Sep 6, 2022
1 parent 0becbd7 commit daa43bc
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 190 deletions.
90 changes: 1 addition & 89 deletions grafana/provisioning/dashboards/main-dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,94 +125,6 @@
"title": "Mobile signal stregth",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "woodcamrmprometheus"
},
"description": "From gateway SNMP protocol",
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "decbytes"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 0
},
"id": 15,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [{
"datasource": {
"type": "prometheus",
"uid": "woodcamrmprometheus"
},
"editorMode": "code",
"expr": "sum by (common_name) (increase(ifHCInOctets{ifName='tun1'}[31d])+increase(ifHCOutOctets{ifName='tun1'}[31d]))",
"legendFormat": "{{common_name}}",
"range": true,
"refId": "A"
}],
"title": "Data transferred over 31 rolling days",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
Expand Down Expand Up @@ -381,7 +293,7 @@
"range": true,
"refId": "A"
}],
"title": "Data transmitted 31 rolling days geneko SNMP",
"title": "Data transmitted 31 rolling days",
"type": "timeseries"
},
{
Expand Down
6 changes: 0 additions & 6 deletions prometheus/queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ pg_woodcamrm_stations:
- last_hydro:
usage: "GAUGE"
decription: "Station API water level"
- current_data:
usage: "GAUGE"
description: "Data plan usage"
- ping_alert:
usage: "COUNTER"
description: "Last ping didn't respond"
- current_daymode:
usage: "GAUGE"
description: "Current night/day mode"
Expand Down
4 changes: 0 additions & 4 deletions woodcamrm/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ def seed_db():
download_records = Jobs(job_name = 'download_records',
full_name = 'Download records',
description = 'Archive records to FTP server and remove temporary clips.')
check_data_plan = Jobs(job_name = 'check_data_plan',
full_name = 'Check data plan',
description = 'Estimate 4G data plan usage.')

dbsql.session.add(default_user)
dbsql.session.add(alive_check)
dbsql.session.add(hydrodata_update)
dbsql.session.add(records_check)
dbsql.session.add(download_records)
dbsql.session.add(check_data_plan)

dbsql.session.commit()

Expand Down
87 changes: 0 additions & 87 deletions woodcamrm/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,93 +112,6 @@ def hydrodata_update():
dbsql.session.commit()


@scheduler.task(
"interval",
id="check_data_plan",
seconds=60 * 60,
max_instances=1,
start_date="2022-01-01 12:00:00",
)
def check_data_plan():
"""Retrieve data usage from router using SNMP protocol."""
with scheduler.app.app_context():
jb = Jobs.query.filter_by(job_name="check_data_plan").first()
jb.last_execution = datetime.now()
jb.state = 0
dbsql.session.commit()

stations = Stations.query.all()

for st in stations:
# Check if station IP is informed
if st.ip and (st.snmp_received or st.snmp_transmitted) and not st.ping_alert:
total = 0

# Loop over the two OIDs (transmitted and received)
for oid in [st.snmp_received, st.snmp_transmitted]:
# Retrieve value from SNMP agent
iterator = getCmd(
SnmpEngine(),
CommunityData("public", mpModel=0),
UdpTransportTarget((st.ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)
jb.last_execution = datetime.now()
jb.state = 1
dbsql.session.commit()
continue
elif errorStatus:
print(
"%s at %s"
% (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
)
jb.last_execution = datetime.now()
jb.state = 1
dbsql.session.commit()
continue
else:
# Add value to total
total += int(varBinds[0][1])

# Convert total from bytes to Mb
total = total * 10**-6

if not st.last_data:
st.last_data = 0

# If router total data is lower than last value, the router has probably rebooted
if total < st.last_data:
st.last_data = total
total += float(st.current_data)
else:
to_substract = st.last_data
st.last_data = total
total -= float(to_substract)
total += float(st.current_data)

# Update stations table on the database
st.current_data = total
st.last_data_check = datetime.now()
dbsql.session.commit()

# Update the jobs table in the database
jb.last_execution = datetime.now()
if jb.state == 0:
jb.state = 4
jb.message = 'running'
dbsql.session.commit()

#TODO: Task to reset data plan

@scheduler.task(
"interval",
id="alive_check",
Expand Down
4 changes: 0 additions & 4 deletions woodcamrm/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def add():

scheduler.get_job(id="hydrodata_update").modify(
next_run_time=datetime.now())
scheduler.get_job(id="check_data_plan").modify(
next_run_time=datetime.now())
scheduler.get_job(id="alive_check").modify(
next_run_time=datetime.now())

Expand Down Expand Up @@ -145,8 +143,6 @@ def update(id):

scheduler.get_job(id="hydrodata_update").modify(
next_run_time=datetime.now())
scheduler.get_job(id="check_data_plan").modify(
next_run_time=datetime.now())
scheduler.get_job(id="alive_check").modify(
next_run_time=datetime.now())
scheduler.get_job(id="records_check").modify(
Expand Down

0 comments on commit daa43bc

Please sign in to comment.