All pastes #212862 Raw Edit

Untitled

public text v1 · immutable
#212862 ·published 2006-10-21 02:18 UTC
rendered paste body
__module_name__ = "Sysinfo"
__module_version__ = "0.1"
__module_description__ = "foo"

import commands
import xchat
print 'Loaded Sysinfo'
title = '\x02'
endtitle = '\x02'
separator = '\x0305 | \x03'
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)', '')
		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():
	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' + '&#39;&#9600;' * freelines + '\x0304' + '&#39;&#9600;' * (14 - freelines) + '\x03\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():
	raw = commands.getoutput('sensors').split('\n')
	cpu, case = '', ''
	for line in raw:
		if line.startswith('CPU'):
			cpu = line.split(':')[1].strip().split('(')[0].strip()
		if line.startswith('Case'):
			case = line.split(':')[1].strip().split('(')[0].strip()
	return cpu, case

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):
	hostname = getHost()
	distro, release, codename, kernel = getVersion()
	speed, computer, processors = getCPU()
	total, free, memgraph = getMemory()
	videocard = getVideoCard()
	res, depth = getVideoRes()
	total_disk, avail_disk, percent_free = getDisk()
	down, up = getNet()
	power_mode, battery = getBattery()
	output = '%sComputador:%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 += '%sProcesador:%s %s' % (title, endtitle, data)
	if processors > 1:	
		output += '%sx @ ' % str(processors)
	output += speed + enddata + separator
	output += '%sModelo:%s %s%s%s%s' % (title, endtitle, data, computer, enddata, separator)
	output += '%sMemoria:%s %s%s%s %sLibre:%s %s%s %s%s%s' % (title, endtitle, data, total, enddata, title, endtitle, data, free, memgraph, enddata, separator)
	output += '%sEspacio en Disco:%s %s%s%s %sLibre:%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 += '%sRed:%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)
	if len(word) == 2 and word[1] == 'sensors':
		cpu_temp, case_temp = getSensors()
		output = '%sSensors:%s %sCPU Temp: %s Case Temp: %s%s' % (title, endtitle, data, cpu_temp, case_temp, enddata)
		xchat.command('say %s' % output)
	return xchat.EAT_ALL

xchat.hook_command('sysinfo', sysinfo_cb, help='')