Monthly Archives: August 2009

Sysinfo plugin for xchat2 on linux

It’s my first time writing something in perl, the code is not very efficient and for now it can only display info of cpu, memory, bandwidth and uptime.

# Configuration
$eth = "eth0";

# Script
Xchat::register("Sysinfo", "0.1", "System Information Displayer");
Xchat::print("Sysinfo 0.1 Loaded.");
Xchat::hook_command("uptime", "uptime");
Xchat::hook_command("os", "os");
Xchat::hook_command("bw", "bw");
Xchat::hook_command("cpu", "cpu");
Xchat::hook_command("mem", "mem");

sub uptime {
	$uptime = `cat /proc/uptime | awk '{printf \$1}'`;
	$_day = 60 * 60 * 24;
	$_hour = 60 * 60;
	$_minute = 60;
	$day = int($uptime/$_day);
	$uptime %= $_day;
	$hour = int($uptime/$_hour);
	$uptime %= $_hour;
	$minute = int($uptime/$_minute);
	$uptime %= $_minute;
	$second = int($uptime);
	$uptime = "";
	if($day != 0) {
		$uptime .= $day;
		if($day == 1) {
			$uptime .= " day ";
		} else {
			$uptime .= " days ";
		}
	}
	if($hour != 0) {
		$uptime .= $hour;
		if($hour == 1) {
			$uptime .= " hour ";
		} else {
			$uptime .= " hours ";
		}
	}
	if($minute != 0) {
		$uptime .= $minute;
		if($minute == 1) {
			$uptime .= " minute ";
		} else {
			$uptime .= " minutes ";
		}
	}
	$uptime .= $second;
	if($second == 1) {
		$uptime .= " second";
	} else {
		$uptime .= " seconds";
	}
	Xchat::command("SAY \002[UPTIME]\002 $uptime");
	Xchat::EAT_ALL;
}

sub os {
	$_kernel_release = `uname -r`;
	$_operating_system = `uname -o`;
	chomp($_kernel_release);
	chomp($_operating_system);
	$os = $_operating_system . " " . $_kernel_release;
	Xchat::command("SAY \002[OS]\002 $os");
	Xchat::EAT_ALL;
}

sub bw {
	$old = `cat /proc/net/dev | grep $eth | cut -d ":" -f 2 | tr -s " "| cut -d " " -f 1,9`;
	sleep(1);
	$new = `cat /proc/net/dev | grep $eth | cut -d ":" -f 2 | tr -s " "| cut -d " " -f 1,9`;
	@ary_old = split(/\s/, $old);
	@ary_new = split(/\s/, $new);
	$down = $ary_new[0] - $ary_old[0];
	$up = $ary_new[1] - $ary_old[1];
	$bw = "";
	if($down < 1000) {
		$bw .= $down . " B/s Down ";
	} else {
		$bw .= sprintf("%.02f", $down / 1000) . " KB/s Down ";
	}	
	if($up < 1000) {
		$bw .= $up . " B/s Up";
	} else {
		$bw .= sprintf("%.02f", $up / 1000) . " KB/s Up";
	}
	Xchat::command("SAY \002[BW]\002 $bw");
	Xchat::EAT_ALL;
}

sub cpu {
	$model_name = `cat /proc/cpuinfo | grep "model name" | tail -1 | cut -d ":" -f 2 | tr -s " "`;
	$number_cores = `cat /proc/cpuinfo | grep "model name" -c`;
	$freq = `cat /proc/cpuinfo | grep "cpu MHz" | tail -1 | cut -d ":" -f 2 | tr -s " "`;
	$idle = `vmstat | tail -1 | awk '{printf \$15}'`;
	$load = 100 - $idle;
	chomp($model_name);
	chomp($number_cores);
	chomp($freq);
	$cpuinfo = $number_cores . " CPU";
	if($number_cores > 1) {
		$cpuinfo .= "'s";
	}
	$cpuinfo .= " -" . $model_name;
	$cpuinfo .= " @ " . int($freq) . " MHz" . " (" . $load . "% Load)";
	Xchat::command("SAY \002[CPU]\002 $cpuinfo");
	Xchat:EAT_ALL;
}

sub mem {
	$mem_total = `free -m | grep Mem | awk '{printf \$2}'`;
	$mem_used = `free -m | grep - | awk '{printf \$3}'`;
	$swap_total = `free -m| grep Swap | awk '{printf \$2}'`;
	$swap_used = `free -m| grep Swap | awk '{printf \$3}'`;
	$meminfo = "\002[MEM]\002 " . $mem_used . "/" . $mem_total . " MB \002[SWAP]\002 " . $swap_used . "/" . $swap_total . " MB";
	Xchat::command("SAY $meminfo");
	Xchat::EAT_ALL;
}

Download Link Converter / 下載鏈接轉換器

I was extremely bored this afternoon, so here it is, the converter for converting download links among FlashGet, QQ Tornado and Xunle, and also decrypting the real link. The tool is pretty much self-explanatory (and useless) so I guess there’s no need for any manuals or instructions. If you are bored too, you can check out the source below, just don’t tell me how ugly it is, coz I’m fully aware of it! 😀 You can find this converter on the “Tool” page, link is at the top of the page.

今天下午極度無聊, 就隨便寫了個可以在 FlashGet, QQ旋風和迅雷間轉換下載鏈接的小工具, 也可以直接看到真實鏈接. 功能及其直白, 就不提供什麼說明了. 如果你跟我一樣無聊, 以下有代碼奉上. 我知道代碼寫得很醜, 所以就不要提到這點啦! 😀 你可以在”Tool”頁面看到這個工具, 鏈接在頁面頂部.

Continue reading