#!/usr/bin/perl -w
# 2002-07-23 Rafael Skodlar
# 16x2 LCD

use Time::localtime;
use FileHandle;
use Net::Ping;

$TTY = "/dev/ttyS1";
$rhost = "198.144.208.1";
$host = `hostname`;
chomp $host;
$delay = 3;
$lcdcmd = "\xFE";	# general command prefix
$cls = $lcdcmd."\x01"; 	# clear display
$cursorhome = $lcdcmd."\x02".$lcdcmd."\xEF";
$crlf = $lcdcmd."\xC0";
$clear = $cls.$cursorhome;
$myip = "198.144.208.138";
#$myip = `ifconfig|awk "/Bcast/{print \$2}"|awk -F: "{print \$1}"`;

&lcdprint;
exit;

sub lcdprint
{
  $counter = 0;
  system ("stty 9600 <$TTY");
  open (LCDOUT, "> $TTY");
  # Clear display
  LCDOUT->autoflush(1);
  # LCD codes for this serial adapter 

  while ( 1 ) {
    $load = `awk '{print \$1 " " \$2 " " \$3}' /proc/loadavg`;
    chomp $load;
    $tm = localtime;
    $year = $tm->year+1900;
    $month = $tm->mon + 1;
    printf LCDOUT "$clear";
    printf LCDOUT "%s%s%s", $host, $crlf, $myip;
    sleep $delay;
    printf LCDOUT "$clear";
    printf LCDOUT "Local time:$crlf";
    printf LCDOUT "%s-%02d-%02d %02d:%02d\n", $year, $month, $tm->mday, $tm->hour, $tm->min, $crlf;
    sleep $delay;
    printf LCDOUT "$clear";
    printf LCDOUT "Load: $crlf$load";
    sleep $delay;
    # counter used for ping and system temp reading delay
    if ($counter eq 0) {
	ping();
	sys_temp();
	$counter = 10;
    }
    $counter--;
    printf LCDOUT "$clear";
    printf LCDOUT "HDD Temperature:$crlf$disk_temp[0]: $disk_temp[2]";
#print "====== Counter: $counter\nTemp: $disk_temp[0]: $disk_temp[2]\n";
    sleep $delay;
    printf LCDOUT "$clear";
    printf LCDOUT "  Property of:$crlf Rafael Skodlar";
    sleep $delay;
    &diskuse;
  }
  close LCDOUT;
}

# Ping some remote
sub ping {
    return if ($counter ne 0);
    $p = Net::Ping->new() or die "Can't create new ping object: $!\n";
    if ($p->ping($rhost)) {
        printf LCDOUT "$clear";
        printf LCDOUT "%s%s   Ping OK", $rhost, $crlf;
    }
    else {
	printf LCDOUT "$clear";
	printf LCDOUT "%s%s NO Ping!!!", $rhost, $crlf;
	sleep 4;
	&alarm;
    }
    $p->close;
    sleep 3;
}

sub alarm() {
    system("sleep 1");
}

sub sys_temp {
    return if ($counter ne 0);
    $disk_tmp = `hddtemp /dev/hda`;
    @disk_temp = split (/:/, $disk_tmp);
}

sub diskuse {
    @df = `df`;
	printf LCDOUT "$clear";
	printf LCDOUT "Filesystem Use$crlf";
	sleep 1;
    foreach $line (@df) {
	@part = split (/\s+/, $line);
	next if ( $part[0] =~ /^Filesystem|tmpfs/);
	@partition = split (/\//, $part[0]);
	printf LCDOUT "$clear";
	printf LCDOUT "%s %s", $partition[2], $part[5];
	printf LCDOUT "$crlf";
	printf LCDOUT "     %s", $part[4];
	sleep 2;
	$val = chomp $part[4];
	print "ALARM: $part[0] size\n" if ($val > 20);
    }
}
# EOS

