Skip to content

Instantly share code, notes, and snippets.

@hughsaunders
Created January 5, 2013 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughsaunders/4462485 to your computer and use it in GitHub Desktop.
Save hughsaunders/4462485 to your computer and use it in GitHub Desktop.
Munin plugin for monitoring an orange uk phone account (data remaining, current spend).
#munin plugin configuration, on debian this goes in /etc/munin/plugin-conf.d/
[orange]
timeout 240
env.owuser YOUR-PHONENUMBER-HERE
env.owpass YOUR-PASSWORD-HERE
#!/usr/bin/env python3
import urllib
import http.cookiejar
import re
import json
import sys
import os
#Munin plugin module for monitoring balance information from the orange website.
class LoginFailure(Exception):
pass
#Class that handles one item of data that will be requested from orange
#For example: Data remaining, or account balance
class OrangeBalanceDatum:
def __init__(self,name,url,json_path,unit):
self.name=name
self.url=url
self.json_path=json_path
self.unit=unit
def set_response(self,response):
self.response=response
self.response_string="".join([l.decode('utf-8') for l in self.response.readlines()])
self.response_obj=json.loads(self.response_string.strip())
#recurse into json object till we find the value at self.path
@property
def value(self):
value_string=self._value(self.response_obj,self.json_path)
return str(value_string).split()[0]
def _value(self,obj,path):
if path==[]: return obj
return self._value(obj[path[0]],path[1:])
class OrangeWeb:
def __init__(self,username,password):
self.username=username
self.password=password
self.cookie_jar=http.cookiejar.CookieJar()
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie_jar)))
self.url_base="https://web.orange.co.uk"
self.login_page_url=self.url_base+"/r/login/"
self.login_submit_url=self.url_base+"/id/signin.php?rm=StandardSubmit"
self.data_xhr_url='https://www.youraccount.orange.co.uk/sss/ajaxServices/simple/remainingDataPAYM'
self.items=[
OrangeBalanceDatum(
'data'
,'https://www.youraccount.orange.co.uk/sss/ajaxServices/simple/remainingDataPAYM'
,['RemainingDataPAYM','simpleValue']
,'MB')
,OrangeBalanceDatum(
'minutes'
,'https://www.youraccount.orange.co.uk/sss/ajaxServices/simple/remainingMinutesPAYM'
,['RemainingMinutesPAYM','simpleValue']
,'Minutes')
,OrangeBalanceDatum(
'balance'
,'https://www.youraccount.orange.co.uk/sss/ajaxServices/complex/PAYMBalance'
,['PAYMBalance','balanceIncludingVAT']
,'GBP')
]
self.form_data={
'LOGIN':self.username
,'PASSWORD':self.password
,'sid':self.get_cookie('sid')
}
self.logged_in=False
#Find a particular cookie from the jar, if it is there.
def get_cookie(self,string):
try:
return [c[string] for c in self.cookie_jar if string in c ]
except:
return []
#Authenticate with orange
def login(self):
if self.logged_in==True: return
urllib.request.urlopen(self.login_page_url)
response=urllib.request.urlopen(self.login_submit_url,data=urllib.parse.urlencode(self.form_data).encode('utf-8'))
for line in [l.decode('utf-8') for l in response]:
if re.search('your account details',line):
self.logged_in=True
return
self.logged_in=False
raise LoginFailure('login failed')
#Request all the items defined in the constructor from orange.
def request_items(self):
if not self.logged_in: self.login()
responses={}
for item in self.items:
#include null data to send request as post.
response=urllib.request.urlopen(item.url,data="".encode('utf-8'))
item.set_response(response)
return self.items
if __name__ == "__main__":
if len(sys.argv)>1:
if sys.argv[1]=='config':
print('data.warning 50:100')
print('data.critical 0:50')
print('balance.warning 10:30')
print('balance.critical 30')
print('balance.label current_spend')
print('data.label data_mb_left')
print('graph_title orange')
print('graph_category orange')
sys.exit(0)
user=os.environ['owuser']
password=os.environ['owpass']
o=OrangeWeb(user,password)
items=o.request_items()
for item in items:
print(item.name+'.value',item.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment