def cpuid(db): conn = database.connection(db) cursor = conn.execute('select ip,host_id from hosts join ports on hosts.id=ports.host_id and portid=22 and hosts.connect_ssh=1') conn.execute('delete from cpuid') conn.commit() re_vendor_id = re.compile('(^vendor_id)([\s]*):(.*)$') re_model_name = re.compile('(^model\ name)([\s]*):(.*)$') re_cpuMhz = re.compile('(^cpu\ MHz)([\s]*):(.*)$') re_cache = re.compile('(^cache\ size)([\s]*):(.*)$') re_physical_id = re.compile('(^physical\ id)([\s]*):(.*)$') for host in cursor: ncores = 0 physicalid=-1 ip = host[0] host_id = host[1] try: ssh = ssh_connect.ssh_22(ip) except: continue stdin, stdout, stderr = ssh.exec_command('cat /proc/cpuinfo') for line in stdout.readlines(): vendor_id = re_vendor_id.match(line) model_name = re_model_name.match(line) cpuMhz = re_cpuMhz.match(line) cache = re_cache.match(line) physical_id = re_physical_id.match(line) if vendor_id: vendorid = vendor_id.group(3) if model_name: modelname = model_name.group(3) if cpuMhz: cpumhz = cpuMhz.group(3) if cache: ccache = cache.group(3) if physical_id: old_phyid = physicalid physicalid = physical_id.group(3) if (old_phyid == physicalid): ncores+=1 conn.execute ('update cpuid set ncore=? where host_id=? and physical_id=?', (ncores, host_id, physicalid,)) else: ncores=1 conn.execute ('insert into cpuid (host_id, vendor_id, model_name,cpuMhz, cache, ncore,physical_id) values (?,?,?,?,?,?,?)',(host_id,vendorid,modelname,cpumhz,ccache,ncores,physicalid,)) if physicalid == -1: ncores=1 conn.execute ('insert into cpuid (host_id, vendor_id, model_name,cpuMhz, cache, ncore,physical_id) values (?,?,?,?,?,?,?)',(host_id,vendorid,modelname,cpumhz,ccache,ncores,1,)) conn.commit() cursor.close()
def loadavg(db): conn = database.connection(db) cursor = conn.execute('select ip,host_id from hosts join ports on hosts.id=ports.host_id and portid=22 and hosts.connect_ssh=1') conn.execute('delete from loadavg') conn.commit() for host in cursor: ip = host[0] host_id = host[1] try: ssh = ssh_connect.ssh_22(ip) except: continue stdin, stdout, stderr = ssh.exec_command('cat /proc/loadavg| cut -f1-3 -d" "') output = stdout.read() load1,load5,load15 = output.split() conn.execute('insert into loadavg (host_id, load1, load5, load15) values (?,?,?,?)', (host_id, load1, load5, load15,)) conn.commit() cursor.close()
def update(db): conn = database.connection(db) cursor = conn.execute('select ip,osdistribution, osrelease, uname, hostname from hosts join ports on hosts.id=ports.host_id and portid=22') ## Some check for open ports or not for host in cursor: ip,osdistribution,osrelease,uname,hostname=host try: ssh = ssh_connect.ssh_22(ip) except: conn.execute('update hosts set connect_ssh=0 where ip=?', (ip,)) print ("Host not accesible: ", ip) continue try: conn.execute('update hosts set connect_ssh=1 where ip=?', (ip,)) stdin, stdout, stderr = ssh.exec_command('cat /etc/debian_version') if not stderr.read(): new_osrelease=stdout.read().strip() if (new_osrelease and ( osrelease!=new_osrelease or "2"!=osdistribution)): conn.execute('update hosts set osdistribution=?, osrelease=? where ip=?', ("2", new_osrelease,ip,)) else: continue stdin, stdout, stderr = ssh.exec_command('cat /etc/redhat-release') new_osrelease=stdout.read().strip() if not stderr.read(): if (new_osrelease and (osdistribution!="1" or osrelease!=new_osrelease)): conn.execute('update hosts set osdistribution=?, osrelease=? where ip=?', ("1", new_osrelease,ip,)) conn.commit() stdin, stdout, stderr = ssh.exec_command('uname -a') new_uname = stdout.read().strip() stdin, stdout, stderr = ssh.exec_command('uname -m') new_arch = stdout.read().strip() stdin, stdout, stderr = ssh.exec_command('hostname -f') new_hostname = stdout.read().strip() if (uname!=new_uname or hostname!=new_hostname or arch != new_arch ): conn.execute('update hosts set uname=?, hostname=?,arch=? where ip=?', (new_uname, new_hostname, new_arch, ip,)) conn.commit() ssh.close except: None cursor.close()
def backup(db): conn = database.connection(db) cursor = conn.execute('select ip,id from hosts where (osdistribution=1 or osdistribution=2) or connect_ssh=1') conn.execute('delete from backup') conn.commit() for host in cursor: ip = host[0] host_id = host[1] try: ssh = ssh_connect.ssh_22(ip) except: continue stdin, stdout, stderr = ssh.exec_command('pgrep caagentd') output = stdout.read() if not output: active=0 else: active=1 conn.execute('insert into backup (hostid, active) values (?,?)', (host_id, active,)) conn.commit() cursor.close()
def disk_use(db): conn = database.connection(db) cursor = conn.execute('select ip,host_id from hosts join ports on hosts.id=ports.host_id and portid=22 and hosts.connect_ssh=1') conn.execute('delete from disks') conn.commit() for host in cursor: ip = host[0] host_id = host[1] try: ssh = ssh_connect.ssh_22(ip) except: continue stdin, stdout, stderr = ssh.exec_command('df -Ph | grep ^/') output = stdout.read() for a in output.split("\n"): if not a: continue device,size,used,available,use,mount = a.split() usenpc = use.split("%")[0] conn.execute('insert into disks (host_id, device, size,used, available, use,mount) values (?,?,?,?,?,?,?)', (host_id, device,size,used,available,usenpc,mount,)) conn.commit() cursor.close()
def meminfo(db): conn = database.connection(db) cursor = conn.execute('select ip,host_id from hosts join ports on hosts.id=ports.host_id and portid=22 and hosts.connect_ssh=1') conn.execute('delete from mem') conn.commit() re_memory_free = re.compile('(^MemFree:)([\s]*)([0-9\s]+)([kmKMBbytesYTES]+)$') re_memory_total = re.compile('(^MemTotal:)([\s]*)([0-9\s]+)([kmKMBbytesYTES]+)$') re_swap_free = re.compile('(^SwapFree:)([\s]*)([0-9\s]+)([kmKMBbytesYTES]+)$') re_swap_total = re.compile('(^SwapTotal:)([\s]*)([0-9\s]+)([kmKMBbytesYTES]+)$') for host in cursor: ip = host[0] host_id = host[1] try: ssh = ssh_connect.ssh_22(ip) except: continue stdin, stdout, stderr = ssh.exec_command('cat /proc/meminfo') memoryFree = memoryTotal = swapFree = swapTotal = 0 memory_free = memory_total = swap_free = swap_total = 0 for line in stdout.readlines(): memory_free = re_memory_free.match(line) memory_total = re_memory_total.match(line) swap_free = re_swap_free.match(line) swap_total = re_swap_total.match(line) if memory_free: memoryFree = int(memory_free.group(3)) memoryFUnit = memory_free.group(4) if memory_total: memoryTotal = int(memory_total.group(3)) memoryTUnit = memory_total.group(4) if swap_free: swapFree = int(swap_free.group(3)) swapFUnit = swap_free.group(4) if swap_total: swapTotal = int(swap_total.group(3)) swapTUnit = swap_total.group(4) conn.execute('insert into mem (host_id, mem_total, mem_free,swap_total,swap_free) values (?,?,?,?,?)', (host_id, memoryTotal, memoryFree, swapTotal, swapFree,)) conn.commit() cursor.close()
def update(db): conn = database.connection(db) conn.execute('delete from packages') conn.commit() cursor=conn.execute('select ip,id from hosts where osdistribution="2" and connect_ssh=1') for ip in cursor: try: ssh = ssh_connect.ssh_22(ip[0]) stdin, stdout, stderr = ssh.exec_command("dpkg -l | grep ^ii | awk '{print $2, $3}'") output=stdout.read() for a in output.split("\n"): if not a: continue pkgname, pkgversion = a.split(" ") cursor = conn.execute ('select * from packages where pkg_host_id=? and pkg_name=?', (ip[1], pkgname,)) if not cursor.fetchone(): conn.execute ('insert into packages (pkg_name, pkg_version, pkg_host_id) values (?,?,?)', (pkgname, pkgversion, ip[1],)) else: conn.execute('update packages set pkg_version=? where pkg_name=? and pkg_host_id=?', (pkgversion, pkgname, ip[1],)) except: None conn.commit()