Finished reworking sc200-pull; removed db code.

This commit is contained in:
Adrian Woodley
2016-12-19 16:22:44 +08:00
parent f21abf7aaf
commit 944243660b

View File

@@ -91,6 +91,9 @@ Settings = [
] + \
['Alarm-Name:' + repr(x) for x in range(59)] + \
['Alarm-Severity:' + repr(x) for x in range(59)]
#Settings = [
#'Site-Name'
#]
def Setup():
@@ -177,21 +180,14 @@ class Worker(threading.Thread):
def run(self):
retry = False
socket.setdefaulttimeout(5)
#print ('Started')
while 1:
IP = self.__IPs.get()
#print((self.getName()))
if IP is None:
self.__Results.put(None)
#print('Exiting')
break
#s = socket.socket()
#if s.connect_ex((str(IP), 80)) == 0:
if GetStatusCode(str(IP), "/languages") == 200:
print((self.getName() + ' processing: ' + str(IP)))
#print(('Socket OK: %s' % IP))
#print(('%s' % IP))
while True:
proxy = ServerProxy('http://%s/xmlrpc' % IP)
try:
@@ -207,69 +203,15 @@ class Worker(threading.Thread):
if len(Values) > 0:
Values.insert(0, str(IP))
self.__Results.put(['SC200', Values])
#AlarmNames = ['Alarm-Name:' + repr(x) for x in range(59)]
#while True:
#try:
#Values = proxy.db.get(AlarmNames)
#except socket.timeout:
#retry = True
#continue
#if retry:
#retry = False
#else:
#break
#Values.insert(0, str(IP))
#self.__Results.put(['AlarmNames', Values])
#AlarmSeverities = ['Alarm-Severity:' + repr(x)
#for x in range(59)]
#Values = proxy.db.get(AlarmSeverities)
#Values.insert(0, str(IP))
#self.__Results.put(['AlarmSeverities', Values])
#s.close()
self.__Results.put(Values)
else:
print((self.getName() + ' skipping: ' + str(IP)))
def Loader():
Count = WORKERS
c = db.cursor()
while 1:
Row = Results.get()
if Row is None:
if Count > 1:
Count -= 1
print(Count)
else:
break
else:
#print(('Loading: %s %s' % (Row[1][0], Row[0])))
#SQL = 'INSERT INTO %s VALUES (' % Row[0]
#for i in Row[1]:
#SQL += '?,'
#SQL = SQL[:-1]
#SQL += ')'
#c.execute(SQL, Row[1])
#print((list(zip(Settings, Row[1][1:]))))
c.executemany('INSERT INTO SettingsValues VALUES ( \'%s\', ?, ?)'
% Row[1][0],
list(zip(Settings, Row[1][1:])))
db.commit()
def Output():
c = db.cursor()
Count = WORKERS
fieldNames = ['IP'] + Settings
with open('/tmp/sc200.csv', 'w', newline='') as outFile:
csvWriter = csv.DictWriter(outFile, fieldnames=fieldNames,
delimiter=',', quotechar='"',
@@ -277,45 +219,20 @@ def Output():
csvWriter.writeheader()
c.execute('''SELECT DISTINCT IP FROM SettingsValues ORDER BY IP''')
for Device in c:
IP = Device
print((('''SELECT SettingsID, Value
FROM SettingsValues WHERE IP = \'%s\'''' % IP)))
row = c.execute('''SELECT SettingsID, Value
FROM SettingsValues WHERE IP = \'%s\'''' % IP)
print(((row)))
while 1:
Row = Results.get()
if Row is None:
if Count > 1:
Count -= 1
#print(Count)
else:
break
else:
csvWriter.writerow(dict(list(zip(fieldNames, Row))))
outFile.close()
#csvWriter.writerow()
#c = db.cursor()
#with open('/tmp/sc200.csv', 'w', newline='') as outFile:
#csvWriter = csv.writer(outFile, delimiter=',', quotechar='"',
#quoting=csv.QUOTE_MINIMAL)
#c.execute('''SELECT * FROM SettingsValues ORDER BY IP''')
#csvWriter.writerow([col[0] for col in c.description])
#for row in c:
#csvWriter.writerow(row)
#outFile.close()
#with open('/tmp/sc200-alarms.csv', 'w', newline='') as outFile:
#csvWriter = csv.writer(outFile, delimiter=',', quotechar='"',
#quoting=csv.QUOTE_MINIMAL)
#c.execute('''SELECT * FROM AlarmNames LIMIT 1''')
#for row in c:
#csvWriter.writerow(row)
#c.execute('''SELECT * FROM AlarmSeverities''')
#for row in c:
#csvWriter.writerow(row)
#outFile.close()
db = sqlite3.connect(':memory:')
#db = sqlite3.connect('/tmp/sc200.db')
Setup()
IPs = queue.Queue(0)
Results = queue.Queue(0)
@@ -328,7 +245,6 @@ for SubNet in Nets:
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):
@@ -337,7 +253,6 @@ for i in range(WORKERS):
if threading.current_thread() is not threading.main_thread():
sys.exit(0)
Loader()
Output()
sys.exit(0)