__module_name__ = "Sysinfo" __module_version__ = "0.2" __module_description__ = "foo" import commands import xchat print 'Loaded Sysinfo' title = '\x02' endtitle = '\x02' separator = ' | ' data = '' enddata = '' def getVersion(): lsb = open('/etc/lsb-release') distro, release, codename = '', '', '' for line in lsb.readlines(): if line.startswith('DISTRIB_ID'): distro = line.split('=')[1][:-1] if line.startswith('DISTRIB_RELEASE'): release = line.split('=')[1][:-1] if line.startswith('DISTRIB_CODENAME'): codename = line.split('=')[1][:-1] raw = open('/proc/version').read() kernel = raw.split()[2] return distro, release, codename, kernel def getCPU(): raw = open('/proc/cpuinfo') speed = '' computer = '' processor = 0 for line in raw.readlines(): if line.startswith('clock'): mhz = float(line.split(':')[1].replace('MHz', '').strip()) if mhz < 1000: speed = ('%.2f' % mhz) + 'Mhz' else: speed = ('%.2f' % (mhz/1000)) + 'Ghz' if line.startswith('detected as'): computer = line.split(':')[1].strip().split(' ', 1)[1][1:-1] if line.startswith('cpu MHz'): mhz = float(line.split(':')[1].strip()) if mhz < 1000: speed = ('%.2f' % mhz) + 'Mhz' else: speed = ('%.2f' % (mhz/1000)) + 'Ghz' if line.startswith('model name'): computer = line.split(':')[1].strip() while computer.find(' ') != -1: computer = computer.replace(' ', ' ') computer = computer.replace('Genuine ', '') computer = computer.replace('(R)', '') computer = computer.replace('(tm)', '') computer = computer.replace('(TM)', '') computer = computer.replace('CPU ', '') if line.startswith('processor'): processor += 1 try: raw = int(open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq').read()) temp = ('%.2f' % (raw/1000000)) + 'Ghz' if temp != '0.00Ghz': speed = temp except: pass return speed, computer, processor def getMemory(no_color): raw = open('/proc/meminfo') total, free = '', '' kbfree = 0 for line in raw.readlines(): if line.startswith('MemTotal'): kbtotal = int(line.split(':')[1].replace('kB', '').strip()) total = str(kbtotal/1024) + 'MB' if line.startswith('Cached'): kbfree += int(line.split(':')[1].replace('kB', '').strip()) if line.startswith('MemFree'): kbfree += int(line.split(':')[1].replace('kB', '').strip()) if line.startswith('Buffers'): kbfree += int(line.split(':')[1].replace('kB', '').strip()) free = str(kbfree/1024) + 'MB' freelines = int(float('%.1f' % (float(kbfree)/kbtotal)) * 14) if freelines == 14: freelines -= 1 memgraph = '\x02[\x02' + '\x0303' + '\xe2\x96\x80' * freelines + '\x0304' + '\xe2\x96\x80' * (14 - freelines) + '\x03\x02]\x02' if no_color: memgraph = '\x02[\x02' + '\xe2\x96\x80' * freelines + ' ' * (14 - freelines) + '\x02]\x02' return total, free, memgraph def getVideoCard(): raw = commands.getoutput('lspci | grep -i vga').split('\n')[0] card = raw.split('controller:', 1)[1].strip() card = card.replace('Corporation ', '') card = card.replace('Technologies ', '') card = card.replace('Inc ', '') final = '' for item in card.split('('): item = item.strip() if ')' in item: item = item.split(')')[1].strip() final += item + ' ' card = final.strip() if len(card) > 45: card = card[:45 - len(card)].rsplit(' ', 1)[0] return card def getVideoRes(): raw = commands.getoutput('xdpyinfo | grep dimensions') res = raw.split(':')[1].strip().split()[0] raw = commands.getoutput('xdpyinfo | grep depth\ of\ root\ window') depth = raw.split(':')[1].strip().split()[0] return res, depth def getDisk(): raw = commands.getoutput('df -h').split('\n') size = 0 avail = 0 for line in raw: if line.startswith('/dev/hd') or line.startswith('/dev/sd'): temp = line.split() total = temp[1].replace(',', '.') free = temp[3].replace(',', '.') if total[-1] == 'G': size += float(total[:-1]) * 1024 elif total[-1] == 'M': size += float(total[:-1]) if free[-1] == 'G': avail += float(free[:-1]) * 1024 elif free[-1] == 'M': avail += float(free[:-1]) return '%.2f' % (size/1024) + 'GB', '%.2f' % (avail/1024) + 'GB', '%.0f' % (avail/size*100) + '%' def getNet(): raw = commands.getoutput('cat /proc/net/dev').split('\n')[2:] in_bytes = 0.0 out_bytes = 0.0 for line in raw: temp = line.split(':')[1].split() in_bytes += float(temp[0].replace(',', '.')) out_bytes += float(temp[8].replace(',', '.')) if (in_bytes/1024/1024) > 500: in_output = '%.2f' % (in_bytes/1024/1024/1024) + 'GB' else: in_output = '%.2f' % (in_bytes/1024/1024) + 'MB' if (out_bytes/1024/1024) > 500: out_output = '%.2f' % (out_bytes/1024/1024/1024) + 'GB' else: out_output = '%.2f' % (out_bytes/1024/1024) + 'MB' return in_output, out_output def getSensors(): cpu, case = '', '' if commands.getstatusoutput('/usr/sbin/laptop-detect')[0] == 0: laptop = True raw = commands.getoutput('acpi -V').split('\n') for line in raw: line = line.strip() if line.startswith('Thermal 1'): cpu = line.split(',')[1].strip().split(' ')[0] + '\xc2\xb0' + 'C' if line.startswith('Thermal 2'): case = line.split(',')[1].strip().split(' ')[0] + '\xc2\xb0' + 'C' else: laptop = False raw = commands.getoutput('sensors').split('\n') for line in raw: if line.startswith('CPU Temp'): cpu = line.split(':')[1].strip().split('(')[0].strip()[1:] if line.startswith('M/B Temp'): case = line.split(':')[1].strip().split('(')[0].strip()[1:] return cpu, case, laptop def getHost(): return commands.getoutput('hostname').strip() def getBattery(): mode = 'ac' battery = None if commands.getstatusoutput('/usr/sbin/laptop-detect')[0] == 0: raw = commands.getoutput('acpi -V').split('\n') for line in raw: line = line.strip() if line.startswith('Battery'): battery = line.split(',')[1].strip() if line.startswith('AC Adapter'): temp = line.split(':')[1].strip() if temp == 'off-line': mode = 'battery' return mode, battery def sysinfo_cb(word, word_eol, userdata): no_color = True if 'color' in word or 'colour' in word: no_color = False separator = '\x0305 | \x03' else: separator = ' | ' if 'sensors' in word: cpu_temp, case_temp, laptop = getSensors() if laptop: output = '%sSensors:%s %sCPU Temp: %s%sCase Temp: %s%s' % (title, endtitle, data, cpu_temp, separator, case_temp, enddata) else: output = '%sSensors:%s %sCPU Temp: %s%sMobo Temp: %s%s' % (title, endtitle, data, cpu_temp, separator, case_temp, enddata) xchat.command('say %s' % output) else: hostname = getHost() distro, release, codename, kernel = getVersion() speed, computer, processors = getCPU() total, free, memgraph = getMemory(no_color) videocard = getVideoCard() res, depth = getVideoRes() total_disk, avail_disk, percent_free = getDisk() down, up = getNet() power_mode, battery = getBattery() output = '%sComputer:%s %s%s%s%s' % (title, endtitle, data, hostname, enddata, separator) output += '%sDistro:%s %s%s %s "%s"%s%s' % (title, endtitle, data, distro, release, codename, enddata, separator) output += '%sProcessor:%s %s' % (title, endtitle, data) if processors > 1: output += '%sx @ ' % str(processors) output += speed + enddata + separator output += '%sModel:%s %s%s%s%s' % (title, endtitle, data, computer, enddata, separator) output += '%sMemory:%s %s%s%s %sFree:%s %s%s %s%s%s' % (title, endtitle, data, total, enddata, title, endtitle, data, free, memgraph, enddata, separator) output += '%sDiskspace:%s %s%s%s %sFree:%s %s%s%s%s' % (title, endtitle, data, total_disk, enddata, title, endtitle, data, avail_disk, enddata, separator) output += '%sVideo:%s %s%s @ %s (%s bpp)%s%s' % (title, endtitle, data, videocard, res, depth, enddata, separator) output += '%sNet:%s %sDown: %s Up: %s%s' % (title, endtitle, data, down, up, enddata) if power_mode == 'battery' and battery != None: output += '%s%sBattery:%s %s%s%s' % (separator, title, endtitle, data, battery, enddata) xchat.command('say %s' % output) return xchat.EAT_ALL xchat.hook_command('sysinfo', sysinfo_cb, help='')