def check_perfdata(self): """ Check performance data (Load average, IO and interface rates) """ def continueParse(vmstat): # Read off /proc/net/dev it = Utils.getIFStat() str = ','.join(["%s:%s:%s" % (i[0], i[1][0], i[1][1]) for i in it.items()]) self.sendPerfdata('ifaces', str) # Read off uptime and IO l = open('/proc/loadavg').read() loads = l.split() # Proccess our vmstat output vms = vmstat.split('\n')[2].split() str = "%s:%s:%s:%s:%s" % (loads[0], loads[1], loads[2], vms[8], vms[9]) self.sendPerfdata('ioload', str) return WebUtils.system('vmstat').addCallback(continueParse)
def runningProcesses(self): def ret(r): status = { 'dhcpd3': False, 'squid': False, 'openvpn': False, 'exim4': False, 'smbd': False, 'sshd': False, } for n in status.keys(): if n in r: status[n] = True return status return WebUtils.system("ps axg -www").addBoth(ret)
def lanTest(self): ha = self.sysconf.General.get('ha', {}) flan = Utils.getLans(self.sysconf)[0] lanIP = self.sysconf.EthernetDevices[flan].get('network', '192.168.0.0/24') myIP = self.sysconf.EthernetDevices[flan].get( 'ip', '192.168.0.0/24').split('/')[0] print lanIP, myIP def lanScan(res): print res for i in res.split('\n'): if i[:5] != "Host:": continue n = i.split() ip = n[1] host = n[2][1:-1] if ip in ha.keys(): ha[ip]['status'] = 'Online' else: ha[ip] = { 'status': 'Online', 'topology': "Not Configured", 'name': host or ip, } if ip == myIP: ha[ip]['status'] = "Me" ha[ip]['name'] = "" servers = [ map(unicode, [v['name'], v['topology'], v.get('status', 'Offline'), k]) for k, v in ha.items() ] return servers loc = 'nmap -sS -p 9682,54322 -oG - %s 2>&1 | grep "9682/open/tcp" | grep "54322/open/tcp"' % ( lanIP.encode("ascii", "replace")) print loc return WebUtils.system(loc).addBoth(lanScan)
def render_ethIP(self, ctx, data): """ Render the IP addresses configured on this interface """ iface = data def ret(ipadd): ips = [] for i in ipadd.strip('\n').split('\n'): ln = i.strip('\n').strip() if not ln: continue ips.append(i.split()[1]) if ips: return ctx.tag[[[ip, tags.br] for ip in ips]] else: return ["None assigned"] return WebUtils.system('ip addr show dev %s | grep "inet "' % iface).addBoth(ret)
def render_computers(self, ctx, data): def rendt(proc): comps = [] for i in proc.split('\n'): if i.strip('\n'): name = i.split(':')[0].strip('$') comps.append([name, tags.a(href=url.root.child("Domain").child("DeleteComp").child(name), onclick="return confirm('%s');" % self.text.compConfirm)[tags.img(src="/images/ex.png")]]) Utils.log.msg('%s opened Tools/Computers' % (self.avatarId.username)) return ctx.tag[ tags.h3[tags.img(src='/images/srvman.png'), self.text.compHeading], tags.h3[self.text.compHeadingList], PageHelpers.dataTable([self.text.compName, ''], comps, sortable=True), tags.h3[self.text.compHeadingAdd], tags.directive('form addComputer'), ] return WebUtils.system('getent passwd | grep Computer').addBoth(rendt)
def submitNSForm(self, ctx, form, data): Utils.log.msg('%s changed forwarding nameservers %s' % (self.avatarId.username, repr(data))) forward = data['forward'].replace(' ', '').replace('\n', '').replace( '\r', '').split(',') self.sysconf.ForwardingNameservers = forward gen = self.sysconf.General sysresolv = data['sysresolv'].replace(' ', '').replace('\n', '').replace( '\r', '').split(',') gen['sysresolv'] = sysresolv self.sysconf.General = gen def res(_): return url.root.child('DNS') return WebUtils.restartService('bind').addCallbacks(res, res)
def getExtensionSelect(): userExtensions = [ ('special/CONF', 'Conference'), ('special/PICKUP', 'Call Pickup'), ] for queueName, queue in config.PBX.get('queues', {}).items(): userExtensions.append( ('queue/%s' % queueName, '%s Queue' % queueName.capitalize())) for ivrName, ivr in config.PBX.get('ivr', {}).items(): userExtensions.append( ('ivr/%s' % ivrName, '%s IVR' % ivrName.capitalize())) for user in WebUtils.getUsers(): username = user.split('@')[0] if username in config.PBXExtensions: userExtensions.append(('ext/' + username, username)) return userExtensions
def render_exiqsum(self, ctx, seg): def eximq(stdout): table = [] for l in stdout.split('\n'): line = l.strip().split() if line: num, vol, old, new, domain = tuple(line) table.append((domain, num, vol, old, new, tags.a(href="/auth/MailQueue/Flush/%s/" % domain)["Resend"])) return ctx.tag[tags.h3["Domain queue"], PageHelpers.dataTable(('Domain', 'Number', 'Volume', 'Oldest', 'Newest', ''), table, sortable=True)] return WebUtils.system( 'mailq | exiqsumm | head -n -3 | tail -n +5').addBoth(eximq)
def pingTest(self): def returnTup(d): # We get back 4 sets of data. The first two are international latency, the last two are local # We take the best of each set with lack of packet loss being preffered to latency intlLatency = 9999 intlPacketloss = 101 localLatency = 9999 localPacketloss = 101 cnt = 0 for i in d.split('\n'): if "packet loss" in i: pl = int(i.split(', ')[2].split('%')[0]) if cnt < 3: if pl < intlPacketloss: intlPacketloss = pl else: if pl < localPacketloss: localPacketloss = pl if pl == 100: # We won't get a latency count after this... cnt += 1 if "rtt" in i: l = int(float(i.split(' = ')[-1].split('/')[1])) if cnt < 4: if l < intlLatency: intlLatency = l else: if l < localLatency: localLatency = l cnt += 1 return intlLatency, intlPacketloss, localLatency, localPacketloss pings = [ 'ping -c 3 google.com 2>&1 | grep -E "(loss|rtt)"', 'ping -c 3 yahoo.com 2>&1 | grep -E "(loss|rtt)"', 'ping -c 3 igubu.saix.net 2>&1 | grep -E "(loss|rtt)"', 'ping -c 3 smtp.isdsl.net 2>&1 | grep -E "(loss|rtt)"', ] return WebUtils.system(';'.join(pings)).addCallback(returnTup)
def check_mailq(self): """ Checker for ludicrous mail queues """ def parse(queue): try: vol, size = queue.replace('\n', '').strip().split()[:2] if size[:-2] != '': bsize = int(size[:-2]) else: bsize = 0 except: self.sendAlert( "System PERM_EXTRASPECIAL error. Server has no exim!", 'vol3') return if int(vol) > 400: self.sendAlert( "Large mail backlog detected. %s messages await delivery" % vol, 'vol1', sub="Vulani notice mq=%s " % vol) return if size[-2:] == "KB": bsize = bsize * 1024 if size[-2:] == "MB": bsize = bsize * 1024 * 1024 if size[-2:] == "GB": bsize = bsize * 1024 * 1024 * 1024 else: bsize = bsize self.sendPerfdata('mailqueue', "%s %s" % (bsize, vol)) if bsize > 104857600: # 100 MB. self.sendAlert( "Large mail backlog detected. Queue size has reached %s" % size, 'vol2', sub="Vulani notice mqs=%s " % vol) return self.alerts['vol1'] = False self.alerts['vol2'] = False return WebUtils.system('mailq | exiqsumm | tail -n 2').addBoth(parse)
def renderHTTP(self, ctx): request = inevow.IRequest(ctx) def outputData(fi): request.setHeader("content-type", "image/png") request.setHeader("content-length", str(len(fi))) return fi getArgs = request.args try: chart = WebUtils.createChart(getArgs) except: chart = None if chart: return outputData(chart.read()) return "Invalid type"
def render_status(self, ctx, data): def gotCb(dta): inv, inf, mdl = '', '', '' try: for ln in dta.split('\n'): if not ': ' in ln: continue k, v = ln.split(': ') if k == 'input.frequency': inf = v if k == 'input.voltage': inv = v if k == 'ups.model': mdl = v except: return ctx.tag[dta.split(':')[-1]] return ctx.tag["%s %sV@%sHz" % (mdl, inv, inf)] return WebUtils.system('upsc %s@localhost' % (data)).addBoth(gotCb)
def Burst(_): print "Burst start", _ for i in v: path, detail = i[0] if "ou=People" not in path: continue dom = path.split(',o=')[0].split('ou=People,dc=')[-1].replace( ',dc=', '.') x = WebUtils.serialiseUser(detail, dom) # create a mail resource locator mail = "%s@%s" % (detail['uid'][0], dom) print "User check:", mail self.handler.sendMessage(self.handler.master.hiveName, "user:%s:%s" % (mail, x)) # soften this reactor.callLater(2, self.handler.sendMessage, self.handler.master.hiveName, "usernoburst:+:+")
def render_content(self, ctx, data): Utils.log.msg('%s tested firewall configuration' % (self.avatarId.username)) def Result(result): errors = [] for i in result.split('\n'): if "ERROR" in i: errors.append(tags.div(style='color:#F00')[i.strip()]) if not errors: # Delete any potential error outputs WebUtils.system('rm /usr/local/tcs/tums/shorewallBroken > /dev/null 2>&1 ') return ctx.tag[ tags.h3["Firewall Test Results"], errors or "No Errors", tags.br, tags.br, tags.a(href=url.root.child('Firewall'))['Back to firewall configuration'] ] return WebUtils.system('/usr/local/tcs/tums/configurator --shorewall; shorewall check').addCallback(Result)
def check_eximstats(self): def continueParse(eximstat): st = eximstat.split('\n') received = 0 deliver = 0 reject = 0 try: received = int(st[1].split()[2]) deliver = int(st[2].split()[2]) reject = int(st[3].split()[1]) except: # some hosts do not provide reject stats or are not busy enough pass str = "%s:%s:%s" % (received, deliver, reject) self.sendPerfdata('eximstat', str) return WebUtils.system( 'eximstats -txt -nr -ne -nt /var/log/exim4/mainlog | head -n 11 | tail -n 5' ).addCallback(continueParse)
def check_df(self): def continueParse(df): devs = [] err = False for i in df.split('\n'): try: l = i.split() if self.sysconf.General.get('diskalert', None): if l[5] in self.sysconf.General['diskalert']: level = self.sysconf.General['diskalert'][l[5]] if type(level) is int: # Use a percentage use if int(l[4].strip('%')) > level: self.sendAlert( "Disk utilisation on %s has exceeded threshold of %s%% - current usage is %s" % (l[5], level, l[4]), 'df') err = True else: lev = level[:-1] unit = level[-1].upper() if unit == "M": level = lev * 1024 elif unit == "G": level = lev * 1024 * 1024 if int(l[3]) < level: self.sendAlert( "Disk freespace on %s has gone below %sK - current space is %sK" % (l[5], level, l[3]), 'df') err = True devs.append("%s:%s:%s:%s" % (l[0], l[5], l[1], l[3])) except: pass # Not the right type of line.. self.alerts['df'] = err str = ';'.join(devs) self.sendPerfdata('diskstat', str) return WebUtils.system('df | grep "^/dev/"').addCallback(continueParse)
def bandwidthTest(self): def updateIntl(intl): if "saved" in intl: speed = intl.split('(')[-1].split(')')[0] else: speed = "Zero" self.callRemote('intlSpeed', unicode(speed)) return def intlDl(local): if "saved" in local: speed = local.split('(')[-1].split(')')[0] else: speed = "Zero" self.callRemote('localSpeed', unicode(speed)) intl = "wget --progress=dot ftp://ftp.debian.org/debian/README.mirrors.txt 2>&1 | grep saved; rm README.mirrors.txt" return WebUtils.system(intl).addCallback(updateIntl) loc = "wget --progress=dot ftp://mirror.ac.za/debian/debian/README.mirrors.txt 2>&1 | grep saved; rm README.mirrors.txt" return WebUtils.system(loc).addCallback(intlDl)
def getExtensionSelect(): userExtensions = [ ('special/PICKUP','Call Pickup'), ] for queueName, queue in config.PBX.get('queues', {}).items(): userExtensions.append(('queue/%s' % queueName,'%s Queue' % queueName.capitalize())) for faxName, fax in config.PBX.get('faxing', {}).items(): userExtensions.append(('fax/%s' % faxName, '%s Fax' % faxName.capitalize())) for roomNo, mDet in config.PBX.get('meetme', {}).items(): userExtensions.append(('conf/%s' % roomNo, 'Conference (%s)' % roomNo)) for ivrName, ivr in config.PBX.get('ivr', {}).items(): userExtensions.append(('ivr/%s' % ivrName, '%s IVR' % ivrName.capitalize())) for user in WebUtils.getUsers(): username = user.split('@')[0] if username in config.PBXExtensions: userExtensions.append(('ext/'+username,username)) return userExtensions
def submitAdvForm(self, ctx, form, data): gen = self.sysconf.General # Apply direct selack = data['selack'] maxwindow = data['maxwin'] backlog = data['backlog'] # Override if gige ticked if data['gige']: maxwindow = '16777216' backlog = '250000' hp = { 'max-window': maxwindow, 'backlog': backlog, 'selective-ack': selack, } if not gen.get('tuning', False): # No tuning stanza, so make one gen['tuning'] = {} # apply the tcp high-performance rules gen['tuning']['tcp-hp'] = hp # Add syn cookies to the mix gen['tuning']['syn-cookies'] = data['cookies'] gen['tuning']['proxyarp'] = data['proxyarp'] self.sysconf.General = gen # Fold. def returnn(_): return url.root.child('Network') return WebUtils.system( '/usr/local/tcs/tums/configurator --tuning; sysctl -q -p' ).addCallback(returnn)
def render_content(self, ctx, data): mq = WebUtils.system('getent passwd | grep Computer') def gotResult(proc): comps = [] for i in proc.split('\n'): if i.strip('\n'): name = i.split(':')[0].strip('$') comps.append([name, tags.a(href=url.root.child("Computers").child("Delete").child(name), onclick="return confirm('%s');" % self.text.compConfirm)[tags.img(src="/images/ex.png")]]) return comps res = wait(mq) yield res mq = res.getResult() getComputers = gotResult(mq) Utils.log.msg('%s opened Tools/Computers' % (self.avatarId.username)) yield ctx.tag[ tags.h3[tags.img(src='/images/srvman.png'), self.text.compHeading], tags.h3[self.text.compHeadingList], PageHelpers.dataTable([self.text.compName, ''], getComputers, sortable=True), tags.h3[self.text.compHeadingAdd], tags.directive('form addComputer'), ]
def submitBalance(self, ctx, form, data): opts = [] if data['track']: opts.append("track") if data['balance']: opts.append("balance") if data['loose']: opts.append("loose") if data['gateway']: gateway = data['gateway'] else: gateway = "-" balance = self.sysconf.ShorewallBalance balance.append([data['zone'].encode(), gateway, ','.join(opts)]) self.sysconf.ShorewallBalance = balance def ok(_): return url.root.child('Routing') return WebUtils.system(Settings.BaseDir+'/configurator --shorewall; shorewall restart').addBoth(ok)
def render_routingTable(self, ctx, data): def formatTable(routes): rtab = [] for l in routes.split('\n'): if not l.strip(): continue ln = l.split() src = ln[0] data = {'via': '', 'device': '', 'type': ''} t = "" for n in ln: if t: data[t] = n if n == 'via': t = 'via' data['type'] = "Static" elif n == 'dev': t = 'device' if not data['type']: data['type'] = "Connected" else: t = "" print data if "kernel" in l: data['type'] = "System" rtab.append([ src, data['via'], data['device'], data['type'], ]) return ctx.tag[PageHelpers.dataTable( ['Destination', 'Next hop', 'Interface', 'Type'], rtab), ] return WebUtils.system('ip ro').addBoth(formatTable)
def lanTest(self): def lanScan(res): hosts = {} lastIP = "" ipOrder = [] for i in res.split('\n'): if "Host" in i: if "(" in i: host = i.split()[1] ip = i.split()[2].replace('(', '').replace(')', '') else: ip = i.split()[1] host = ip hosts[ip] = [unicode(host), u"", u""] ipOrder.append(ip) lastIP = ip elif "MAC" in i: mac = i.split()[2] brand = i.split()[3].replace('(', '').replace(')', '') hosts[lastIP][1] = unicode(mac) hosts[lastIP][2] = unicode(brand) bundle = [] for k in ipOrder: v = hosts[k] sortKey = [int(i) for i in k.split('.')] bundle.append((unicode(k), v[0], v[1], v[2])) return bundle loc = [] for k, v in Utils.getLanNetworks(self.sysconf).items(): loc.append('nmap -sP %s 2>&1 | grep -E "(be up|MAC)"' % (v)) return WebUtils.system(';'.join(loc)).addCallback(lanScan)
def returnAuth(res): # Trace back our topology and find our closest interface to this host iface, zone, network, routed = Utils.traceTopology( self.sysconf, host) ipserv = self.sysconf.EthernetDevices[iface]['ip'].split( '/')[0] print res, user, passw, ipserv, iface, zone, network, routed, host if res: print "Ok bitch" # Add our record to the zone def done(mac): print "User has this MAC", mac l = open('/tmp/caportal/%s' % host, 'wt') l.write("%s|%s|%s" % (time.time(), mac.strip('\n'), user)) l.close() os.chmod('/tmp/caportal/%s' % host, 0777) print "Resturning person to ", segments #return url.URL.fromString('http://%s' % ('/'.join(segments[1:]))) return RefreshTo(url='http://%s' % ('/'.join(segments[1:]))) def next(_): print "Added shorewall, going to ARP check" return WebUtils.system( "arp -n | grep %s | awk '{print $3}'" % host).addBoth(done) return WebUtils.system( 'shorewall add %s:%s c%s' % (iface, host, zone)).addBoth(next), () print "Invalid authentication from", user, ":", repr(res) return Portal(self.host, self.url, "Invalid username or password."), ()
def submitConfig(self, ctx, form, data): k = self.sysconf.ProxyConfig if data['bindaddr']: k['bindaddr'] = data['bindaddr'].encode("ascii", "replace") k['captive'] = data['captive'] k['captiveblock'] = data['captiveblock'] k['contentfilter'] = data['contentfilter'] k['updates'] = { 'enabled': data['advanced'], 'maxdisk': '95', 'maxspeed': '0' } self.sysconf.ProxyConfig = k def squidRestart(_): print _ return reloadConfig(url.root.child('Squid')) return WebUtils.system( '/usr/local/tcs/tums/configurator --shorewall; shorewall restart' ).addBoth(squidRestart)
def flushObject(self, name): def flushDb(ret): return self.db[4].deleteFile(name) return WebUtils.system('rm -rf /var/lib/samba/updates/%s' % sha.sha(name).hexdigest()).addBoth(flushDb)
def reloadGuard(result): return WebUtils.system( Settings.BaseDir + '/configurator --cfilter; /etc/init.d/dansguardian restart').addBoth( lambda _: result)
def reloadSquid(_, result): # Call configurator to reconfigure squid d = WebUtils.system('/usr/sbin/squid -k reconfigure > /dev/null 2>&1') return d.addCallback(lambda _: result)
def reloadSamba(self): WebUtils.system(Settings.BaseDir + '/configurator --samba') WebUtils.system("/etc/init.d/samba restart")
def submitKey(self, ctx, f, data): # Generate an SSH key return WebUtils.system( 'rm /root/.ssh/identity; rm /root/.ssh/identity.pub; ssh-keygen -b 1024 -t rsa -N "" -C v2 -f /root/.ssh/identity' ).addBoth(lambda _: url.root.child('HA'))