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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">