#!/usr/bin/python3
from netaddr import IPNetwork
#from netaddr import IPAddress
from xmlrpc.client import ServerProxy
import socket
import queue
import threading
import sys
import http.client

WORKERS = 10

if len(sys.argv) == 1:
    Nets = [
       '10.250.48.0/27',
       '10.250.48.32/27',
       '10.250.48.64/27',
       '10.250.48.96/27',
       '10.250.48.128/27',
       '10.250.48.160/27',
       '10.250.49.0/27',
       '10.250.49.32/27',
       '10.250.49.64/27',
       '10.250.49.128/27',
       '10.250.49.160/27',
       '10.250.50.0/27',
       '10.250.50.32/27',
       '10.250.50.65/27',
       '10.250.50.96/27',
       '10.250.50.128/27',
       '10.250.50.160/27',
       '10.250.50.192/27',
       '10.250.50.224/27',
       '10.250.51.0/27',
       '10.250.51.32/27',
       '10.250.51.64/27',
       '10.250.51.96/27',
       '10.250.51.128/27',
       '10.250.52.0/27',
       '10.250.52.32/27',
       '10.250.52.64/27',
       '10.250.52.96/27',
       '10.250.52.128/27',
       '10.250.52.160/27',
       '10.250.52.192/27'
        ]
else:
    Nets = sys.argv[1:]


def GetStatusCode(host, path="/"):
    try:
        conn = http.client.HTTPConnection(host)
        conn.request("GET", path)
        return conn.getresponse().status
    except Exception:
        return None


class Worker(threading.Thread):

    def __init__(self, IPs):
        self.__IPs = IPs
        self.parent = threading.current_thread()
        threading.Thread.__init__(self)

    def run(self):
        retry = False
        socket.setdefaulttimeout(5)
        while 1:
            IP = self.__IPs.get()

            if IP is None:
                break

            if GetStatusCode(str(IP), "/languages") == 200:
                print((self.getName() + ' processing: ' + str(IP)))
                while True:
                    proxy = ServerProxy('http://%s/xmlrpc' % IP)
                    try:
                        Values = proxy.db.set({
'Smart-Alarm-Name:1': 'Solar',
'Smart-Alarm-Severity:1': 3,
'Smart-Alarm-DO-Mapping-A:1': 1,
'Smart-Alarm-Send-Trap:1': 1,
'Source-Alarm-Status:1': 1,
'Source-Alarm-Mapping:1': 1,
'Source-Schedule-Mapping:1': 1,
'Source-Schedule-Status:1': 1,
'Smart-Alarm-Operator:1': 0,
'Source-Schedule-First-Date-Time:1': 1481673600,
'Source-Schedule-Duration:1': 480,
'Alarm-Severity': 5,
'Alarm-DO-Mapping-A': 0,
'Alarm-Send-Trap': 0,
'Low-Float-Threshold': 50
                       })
                    except socket.timeout:
                        retry = True
                        continue

                    if retry:
                        retry = False
                    else:
                        break
                print((self.getName() + ':' + str(IP) + str(Values)))
            else:
                print((self.getName() + ' skipping: ' + str(IP)))


IPs = queue.Queue(0)

for i in range(WORKERS):
    Worker(IPs).start()

for SubNet in Nets:
    Net = IPNetwork(SubNet)

    for IP in Net:
        if ((IP != Net.network and IP != Net.broadcast) or
        Net.broadcast is None):
            #print(('Queued: ' + str(IP)))
            IPs.put(IP)

for i in range(WORKERS):
    IPs.put(None)

if threading.current_thread() is not threading.main_thread():
    sys.exit(0)

sys.exit(0)

