LCDproc development and user support list

Text archives Help


[Lcdproc] Uptime Perl script?


Chronological Thread 
  • From: speedy_2k2 at hotmail.com (Speedy2k)
  • Subject: [Lcdproc] Uptime Perl script?
  • Date: Sat, 15 Nov 2008 10:44:41 -0500

What you guy's use to make a uptime script in perl, i'm using CentOS5 i would
like to have like this:

D: "days" H: "hours" M: "minutes" S: "seconds"
Exemple.
D:10 H:05 M:15 S:10

And i have another question, i have a shell script that get my external IP,
it look like that:

#!/bin/bash
curl -s http://www.whatismyip.com/automation/n09230945.asp

I don't know if i can incorporate it in my perl script without asking for an
external script. if yes it would be really cool.

And i got a script to take my internal IP to:

#!/bin/bash
IP=`/sbin/ifconfig $1|gawk '/inet addr/{print $2}'|gawk -F: '{ORS="";print
$2}'`
echo -n $IP


I would like to know if i can make those 2 scripts incorporated into my perl
script so i will not need to call external files, and is there a way to ask
for and IP refresh at about 2 hours interval, but make the uptime in real
time? I'm really new to Perl scripting, but i love to make thing by myself,
so if someone can give me a hand on this one it would be really appreciated
thanx!


Here is my perl script so far i use it as a deamon.



#!/usr/bin/perl -w
use strict;
use POSIX qw( setsid );

my $debug = 1;
my $logfile = q(.testlog);

# Season to taste
my @fh_unused = (\*STDIN, \*STDOUT);

open \*STDERR, ">> $ENV{'HOME'}/$logfile";
select((select(\*STDERR), $| = 1)[0]);

{
# Daemon Rule 1) Fork and exit the parent.
my $ppid = $$;
my $pid = fork and exit 0;
! defined $pid and die "No Fork: ", $!;
while (kill 0, $ppid) {
select undef, undef, undef, .001;
};
}

# Daemon Rule 2) become session leader, pg leader, no term
my $session_id = POSIX::setsid();

# Daemon Rule 3) cd to /
chdir '/' or die "Could not cd to rootfs", $!;

# Daemon Rule 4) set file creation mask to 0
my $oldmask = umask 00;

# Daemon Rule 5) Close unneeded file handles
close $_ or die $! for @fh_unused;
################################################################
sleep 20;
use IO::Socket;
use Fcntl;
use strict;
#
my $remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => 13666)|| die "Cannot connect to LCDd\n";
$remote->autoflush(1);
sleep 1; # give the server time to notice us...
print $remote "hello\n";
# we must read the response even if we ignore it:
my $lcdresponse = <$remote>;
# Turn off blocking mode...
fcntl($remote, F_SETFL, O_NONBLOCK);
# Set up some screen widgets...
print $remote "client_set name lcdtime\n";
#Screen_1
print $remote "screen_add scr1\n";
print $remote "widget_add scr1 str1 title\n";
print $remote "widget_add scr1 str2 string\n";
print $remote "widget_add scr1 str3 string\n";
print $remote "widget_add scr1 str4 string\n";
#Screen_2
#print $remote "screen_add scr2\n";
#print $remote "widget_add scr2 str1 title\n";
#print $remote "widget_add scr2 str2 string\n";
#print $remote "widget_add scr2 str3 string\n";
#print $remote "widget_add scr2 str4 string\n";

my $date;
my @ltime = localtime;
my $ystart = sprintf("%04d", 1900+$ltime[5]);
my $mstart = sprintf("%02d", $ltime[4]+1);
my $jstart = sprintf("%02d", $ltime[3]);
my $hstart = sprintf("%02d", $ltime[2]);
my $minstart = sprintf("%02d", $ltime[1]);
my $yup;
my $mup;
my $jup;
my $hup;
my $minup;
my $minnow;
my $extip = `/usr/local/sbin/getextip.sh`;
my $intip = `/usr/local/sbin/getip.sh eth0`;
my $vpnip = `/usr/local/sbin/getip.sh ham0`;
while(1)
{
$lcdresponse = <$remote>;
#$date = scalar localtime; # this is 24 char long. The following is
bettter:
@ltime = localtime;
#return a date in yyyy-mm-dd hh:mm format
$date = sprintf("%04d-%02d-%02d %02d:%02d",
1900+$ltime[5],$ltime[4]+1,$ltime[3],$ltime[2],$ltime[1],$ltime[0]);
####Seter les variable du timer.
$yup = sprintf("%04d", 1900+$ltime[5]) - $ystart;
$mup = sprintf("%02d", $ltime[4]+1) - $mstart;
$jup = sprintf("%02d", $ltime[3]) - $jstart;
$hup = sprintf("%02d", $ltime[2]) - $hstart;
$minup = sprintf("%02d", $ltime[1]) - $minstart;
$minnow = sprintf("%02d", $ltime[1]);
####Uptime

####Screen 1 Widgets
print $remote "widget_set scr1 str1 \"FullIP PBX\"\n";
print $remote "widget_set scr1 str2 1 2 \"$date\"\n";
print $remote "widget_set scr1 str4 1 4 \"A:$yup M:$mup J:$jup H:$hup
M:$minup\"\n";
####Screen 2 Widgets
# print $remote "widget_set scr2 str1 \"Info. Reseau.\"\n";
# print $remote "widget_set scr2 str2 1 2 \"LAN: $intip\"\n";
# print $remote "widget_set scr2 str3 1 3 \"WAN: $extip\"\n";
# print $remote "widget_set scr2 str4 1 4 \"VPN: $vpnip\"\n";
####Screen Printing
print "@ltime\n";
print "$date\n";
print "VPN:$vpnip\n";
print "Local:$intip\n";
print "WAN:$extip\n";
print "A:$yup M:$mup J:$jup H:$hup M:$minup\n";
print "minstart: $minstart\n";
print "minnow: $minnow\n";

sleep 10;
}



exit 0;











-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.omnipotent.net/pipermail/lcdproc/attachments/20081115/6f7ae7da/attachment-0001.htm>




Archive powered by MHonArc 2.6.18.

Top of page