Last active
November 5, 2015 18:20
-
-
Save 112buddyd/4cc574fdd54d5a22a503 to your computer and use it in GitHub Desktop.
Polling script for my netmon tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3, datetime, subprocess, time | |
from multiprocessing.pool import ThreadPool | |
class Device(object): | |
instances = [] | |
def __init__(self, id, hostname, type, ip, subnet, building, online, last_seen): | |
self.id = id | |
self.hostname = hostname | |
self.type = type | |
self.ip_address = ip | |
self.subnet = subnet | |
self.building = building | |
self.online = online | |
self.last_seen = last_seen | |
Device.instances.append(self) | |
def __str__(self): | |
return self.hostname | |
def ping(self): #this probably only works on windows | |
proc = subprocess.Popen(['ping', '-n', '1', self.ip_address], stdout=subprocess.PIPE) | |
stdout, stderr = proc.communicate() | |
if proc.returncode == 0: | |
self.online = 'True' | |
self.last_seen = str(datetime.datetime.now()) | |
else: | |
self.online = 'False' | |
def all(): | |
return str(Device.instances) | |
def is_online(self): | |
return self.online | |
def report_down(): | |
print('{0:15} {1:8} {2:8} {3:20}'.format('Device', 'Location', 'Status', 'Last Seen')) | |
for instance in Device.instances: | |
if instance.online == 'False': | |
print('{0:15} {1:8} {2:8} {3:20}'.format(instance.ip_address, instance.building, 'Offline', instance.last_seen)) | |
def build_objects(): | |
conn = sqlite3.connect('nsbmon.db') | |
c = conn.cursor() | |
devices = c.execute("SELECT * FROM monitor_networkdevice") | |
for row in devices.fetchall(): | |
Device(id = row[0], | |
hostname = row[1], | |
type = row[2], | |
ip = row[3], | |
subnet = row[4], | |
building = row[5], | |
online = row[6], | |
last_seen = row[7]) | |
c.close() | |
def poll_collector(): | |
poll_data = [] | |
for instance in Device.instances: | |
poll_data.append((instance.online, instance.last_seen, instance.id)) | |
return poll_data | |
def update_sql_from_objects(): | |
conn = sqlite3.connect('nsbmon.db') | |
c = conn.cursor() | |
c.executemany('UPDATE monitor_networkdevice SET online = ?, last_seen = ? WHERE id= ?', poll_collector()) | |
print('{} total rows updated in database.'.format(conn.total_changes)) | |
conn.commit() | |
c.close() | |
def poller(threads=250): | |
print('Starting poller, will loop forever. Press Esc to break.') | |
try: | |
while True: | |
start_time = datetime.datetime.now() | |
pool = ThreadPool(threads) | |
pool.map(Device.ping, Device.instances) | |
pool.close() | |
pool.join() | |
end_time = datetime.datetime.now() | |
print('Polled {} devices in {}.'.format(len(Device.instances), (end_time-start_time))) | |
update_sql_from_objects() | |
print('Sleeping for 3 mins.') | |
time.sleep(180) | |
except KeyboardInterrupt: | |
pass | |
build_objects() | |
poller() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment