Dr0ptp4kt has submitted this change and it was merged.

Change subject: Adding MCC-MNC mismatch script.
......................................................................


Adding MCC-MNC mismatch script.

Change-Id: Ibc4d09cebabe67fee3e8ca3c5b133da3b4690d31
---
A scripts/README
A scripts/mccmnc.py
2 files changed, 104 insertions(+), 0 deletions(-)

Approvals:
  Dr0ptp4kt: Verified; Looks good to me, approved



diff --git a/scripts/README b/scripts/README
new file mode 100644
index 0000000..ba5b5d1
--- /dev/null
+++ b/scripts/README
@@ -0,0 +1,47 @@
+== mccmnc.py ==
+
+To run mccmnc.py:
+1. Create a settings file settings/mccmny.josn under this directory.
+
+{
+ "apiPassword": "password",
+ "apiUsername": "username"
+}
+
+2. Ensure you have the dependencies defined in mccmnc.py. On a Mac 
+you might use pip2.7 to install them. This runs with Python 2.7. It's
+usually easiest to try running python mccmnc.py and installing as
+needed. On the event logging server the dependencies are as of
+15-Dec-2014 already installed.
+
+3. If necessary, set your PYTHONPATH variable prior to executing:
+
+export PYTHONPATH=/usr/local/lib/python2.7/site-packages
+
+4. Run using the usage defined in mccmnc.py.
+
+python mccmnc.py dbhost dbname mysql_cnf_path YYMMDD[HH...]
+
+To schedule mccmnc.py, on the event logging server:
+
+Add an email address to a file named "email" in your home directory.
+If you want multiple email addresses, add them space separated.
+
+Create a file mccmnc.sh in your home directory with the following contents:
+
+#! /bin/sh
+
+cd $HOME
+DATE=`date --date="yesterday" "+%Y%m%d"`
+EMAIL=`cat email`
+python mccmnc.py dbhost dbname mysql_cnf_path $DATE | mailx -s "IP Mismatches 
for $DATE" $EMAIL
+
+
+Then add the following to your crontab with crontab -e:
+
+PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
+HOME=/home/username
+# once daily:
+15 14 * * * /home/username/mccmnc.sh &> /dev/null
+
+Of course, replace "username" with your username on the event logging server.
diff --git a/scripts/mccmnc.py b/scripts/mccmnc.py
new file mode 100644
index 0000000..be72488
--- /dev/null
+++ b/scripts/mccmnc.py
@@ -0,0 +1,57 @@
+import json
+import requests as r
+import api
+from logprocessor import ScriptProcessor
+from netaddr import *
+import _mysql
+import sys
+
+'''
+This is not efficient. Let's just get it working first.
+
+Usage: python mccmnc.py dbhost dbname mysql_cnf_path YYMMDD[HH...]
+'''
+
+class MccMncChecks(ScriptProcessor):
+    def __init__(self, settingsFile='settings/mccmnc.json'):
+        super(MccMncChecks, self).__init__(settingsFile, 'mccmnc')
+
+    def run(self):
+        zerowiki = self.getWiki()
+        if self.proxyUrl:
+            zerowiki.session.proxies = self.proxyUrl
+
+        zerowiki.login(self.settings.apiUsername, self.settings.apiPassword)
+        data = zerowiki('zeroportal', type='carriers')
+        toRemove = []
+        for xcs, subnets in data.items():
+            if xcs == '310-260' or xcs.startswith('TEST'):
+                toRemove.append(xcs)
+        for i in toRemove:
+            del data[i]
+
+        
db=_mysql.connect(host=sys.argv[1],db=sys.argv[2],read_default_file=sys.argv[3])
+        db.query("""select event_ip,  event_mccMncNetwork, event_mccMncSim, 
count(*) from MobileWikiAppOperatorCode_8983918 where timestamp like '""" + 
sys.argv[4] + """%' group by event_ip,  event_mccMncNetwork, event_mccMncSim""")
+        results = db.store_result()
+        print ','.join(['supposed','network','sim','ip'])
+        while True:
+            record = results.fetch_row()
+            if not record: break
+            record = record[0]
+            found = False
+            for xcs, subnets in data.items():
+                for subnet in subnets:
+                    if IPAddress(record[0]) in IPNetwork(subnet):
+                        found = True
+                        if record[1] == xcs and record[2] == xcs:
+                            break
+                        print ','.join([xcs,record[1],record[2],record[0]])
+                        break
+                if found:
+                    break
+            if not found and (record[1] in data.keys() or record[2] in 
data.keys()):
+                print ','.join(['unmapped',record[1],record[2],record[0]])
+        db.close()
+
+if __name__ == '__main__':
+    MccMncChecks().safeRun()

-- 
To view, visit https://gerrit.wikimedia.org/r/179926
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ibc4d09cebabe67fee3e8ca3c5b133da3b4690d31
Gerrit-PatchSet: 2
Gerrit-Project: analytics/zero-sms
Gerrit-Branch: master
Gerrit-Owner: Dr0ptp4kt <[email protected]>
Gerrit-Reviewer: Dr0ptp4kt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to