LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-28-2006, 07:26 AM   #1
meniscus
Member
 
Registered: May 2006
Posts: 70

Rep: Reputation: 15
interpreting perl script


I found the following script below rrd_hddtemp.pl to display graphs of my hard drive temperature.I can sort of follow most of it but


What do the following lines mean?

Code:
my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
	# remove eol chars and white space
	$temp =~ s/[\n ]//g;
Also am i right to assume $_[0] $_[1] $_[2] etc...are standard naming conventions for inputs in perl scripting?

Code:
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)
&ProcessHDD("hda", "SAMSUNG SP1213N");
#&ProcessHDD("hdb", "primary slave");
#&ProcessHDD("hdc", "40GB Seagate");
#&ProcessHDD("hdd", "secondary slave");

sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
#         $_[1]: hdd description

	# get hdd temp for master drive on secondary IDE channel
	my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
	# remove eol chars and white space
	$temp =~ s/[\n ]//g;
	
	print "$_[1] (/dev/$_[0]) temp: $temp degrees C\n";

	# if rrdtool database doesn't exist, create it
	if (! -e "$rrd/$_[0].rrd")
	{
		print "creating rrd database for /dev/$_[0]...\n";
		RRDs::create "$rrd/$_[0].rrd",
			"-s 300",
			"DS:temp:GAUGE:600:0:100",
			"RRA:AVERAGE:0.5:1:576",
			"RRA:AVERAGE:0.5:6:672",
			"RRA:AVERAGE:0.5:24:732",
			"RRA:AVERAGE:0.5:144:1460";
	}

	# insert value into rrd
	RRDs::update "$rrd/$_[0].rrd",
		"-t", "temp",
		"N:$temp";

	# create graphs
	&CreateGraph($_[0], "day", $_[1]);
	&CreateGraph($_[0], "week", $_[1]);
	&CreateGraph($_[0], "month", $_[1]);
	&CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: hdd description

	RRDs::graph "$img/$_[0]-$_[1].png",
		"--lazy",
		"--alt-y-grid",
		"-s -1$_[1]",
		"-t hdd temperature :: $_[2] (/dev/$_[0])",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v degrees C",
		"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
		"LINE2:temp#0000FF:$_[2] (/dev/$_[0])",
		"GPRINT:temp:MIN:  Min\\: %2.lf",
		"GPRINT:temp:MAX: Max\\: %2.lf",
		"GPRINT:temp:AVERAGE: Avg\\: %4.1lf",
		"GPRINT:temp:LAST: Current\\: %2.lf degrees C\\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}
 
Old 11-28-2006, 07:30 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
the first line runs a shell command and saves the output to a variable. the other line does exactly what the comment there clearly says, it removes eol chars and white space from the result.
 
Old 11-28-2006, 07:57 AM   #3
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
This is the oringinal script

Code:
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)
#ProcessHDD("hda", "SAMSUNG SP1213N");
#&ProcessHDD("hdb", "primary slave");
&ProcessHDD("hdc", "40GB Seagate");
#&ProcessHDD("hdd", "secondary slave");

sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
#         $_[1]: hdd description

	# get hdd temp for master drive on secondary IDE channel
	my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
	# remove eol chars and white space
	$temp =~ s/[\n ]//g;
	
	print "$_[1] (/dev/$_[0]) temp: $temp degrees C\n";

	# if rrdtool database doesn't exist, create it
	if (! -e "$rrd/$_[0].rrd")
	{
		print "creating rrd database for /dev/$_[0]...\n";
		RRDs::create "$rrd/$_[0].rrd",
			"-s 300",
			"DS:temp:GAUGE:600:0:100",
			"RRA:AVERAGE:0.5:1:576",
			"RRA:AVERAGE:0.5:6:672",
			"RRA:AVERAGE:0.5:24:732",
			"RRA:AVERAGE:0.5:144:1460";
	}

	# insert value into rrd
	RRDs::update "$rrd/$_[0].rrd",
		"-t", "temp",
		"N:$temp";

	# create graphs
	&CreateGraph($_[0], "day", $_[1]);
	&CreateGraph($_[0], "week", $_[1]);
	&CreateGraph($_[0], "month", $_[1]);
	&CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: hdd description

	RRDs::graph "$img/$_[0]-$_[1].png",
		"--lazy",
		"--alt-y-grid",
		"-s -1$_[1]",
		"-t hdd temperature :: $_[2] (/dev/$_[0])",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v degrees C",
		"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
		"LINE2:temp#0000FF:$_[2] (/dev/$_[0])",
		"GPRINT:temp:MIN:  Min\\: %2.lf",
		"GPRINT:temp:MAX: Max\\: %2.lf",
		"GPRINT:temp:AVERAGE: Avg\\: %4.1lf",
		"GPRINT:temp:LAST: Current\\: %2.lf degrees C\\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}



Its written as to monitor temps from hdc( as in

Code:
&ProcessHDD("hdc", "40GB Seagate");
is uncommented


Im looking to monitor hda.
Do i need to alter this following line for it to work (as well as comment appropiately with hda)

Code:
my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
 
Old 11-28-2006, 08:00 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
all you'd want to do is uncomment the rlevant ProcessHDD() line. looks like the & is missing from the hds one that's already there though...
 
Old 11-28-2006, 08:12 AM   #5
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thank u for clarifing that.

It says in the tutorial where i got this script( http://martybugs.net/linux/hddtemp.cgi)

that when i run the script i should see these lines

Code:
[root@thain root]# /usr/local/bin/rrd_hddtemp.pl
40GB Seagate (/dev/hdc) temp: 41 degrees C
creating rrd database for /dev/hdc...
Im getting this output

Code:
meniscus@meniscot:/usr/local/bin$ sudo rrd_hddtemps.pl
SAMSUNG SP1213N (/dev/hda) temp:  degrees C
creating rrd database for /dev/hda...

The temp section is left blank!

When i finish out the tutorial the graphs are being created but theres nothing in them!

Does the script need to be modified somewhere else?
 
Old 11-28-2006, 08:51 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
i don't know the ins and outs of this. you need to manually reproduce the automatic commadns that are being performed and i'm sure things will come clear, e.g. "/usr/local/sbin/hddtemp -n /dev/hda"
 
Old 11-28-2006, 09:11 AM   #7
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
I tried the script command seperate and got 33 (look below)

Code:
meniscus@meniscot:/usr/local/sbin$ sudo hddtemp -n  /dev/hda
Password:
33
It doesnt seem to be recognising "$_[0]" as the drive.
Is "$_[0]" a common naming convention in linux?

Is it this line in the script that is wrong
 
Old 11-28-2006, 09:25 AM   #8
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Is the folder "/usr/local/sbin/hddtemp" in the code supposted to exist because theres nothing in the the sbin folder?
 
Old 11-28-2006, 10:48 AM   #9
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,524

Rep: Reputation: 94
What gives "which hddtemp"? If the path differs adjust the path in your script.
 
Old 11-28-2006, 11:05 AM   #10
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
My Hard drive temperature is accessed by /dev/hda

The script below is written to access /dev/hdc.

Can i confirm the following code, ie

Code:
my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;

accesses the information in /dev/hda?

and this code

Code:
$temp =~ s/[\n ]//g;

extracts just the temperature value out of it?











Code:
#!/usr/bin/perl
#
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_hddtemp.pl

use RRDs;

# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
# define location of images
my $img = '/var/www/html/rrdtool';

# process data for each specified HDD (add/delete as required)
#&ProcessHDD("hda", "primary master");
#&ProcessHDD("hdb", "primary slave");
&ProcessHDD("hdc", "40GB Seagate");
#&ProcessHDD("hdd", "secondary slave");

sub ProcessHDD
{
# process HDD
# inputs: $_[0]: hdd (ie, hda, etc)
#         $_[1]: hdd description

	# get hdd temp for master drive on secondary IDE channel
	my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
	# remove eol chars and white space
	$temp =~ s/[\n ]//g;
	
	print "$_[1] (/dev/$_[0]) temp: $temp degrees C\n";

	# if rrdtool database doesn't exist, create it
	if (! -e "$rrd/$_[0].rrd")
	{
		print "creating rrd database for /dev/$_[0]...\n";
		RRDs::create "$rrd/$_[0].rrd",
			"-s 300",
			"DS:temp:GAUGE:600:0:100",
			"RRA:AVERAGE:0.5:1:576",
			"RRA:AVERAGE:0.5:6:672",
			"RRA:AVERAGE:0.5:24:732",
			"RRA:AVERAGE:0.5:144:1460";
	}

	# insert value into rrd
	RRDs::update "$rrd/$_[0].rrd",
		"-t", "temp",
		"N:$temp";

	# create graphs
	&CreateGraph($_[0], "day", $_[1]);
	&CreateGraph($_[0], "week", $_[1]);
	&CreateGraph($_[0], "month", $_[1]);
	&CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
# creates graph
# inputs: $_[0]: hdd name (ie, hda, etc)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: hdd description

	RRDs::graph "$img/$_[0]-$_[1].png",
		"--lazy",
		"-s -1$_[1]",
		"-t hdd temperature :: $_[2] (/dev/$_[0])",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v degrees C",
		"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
		"LINE2:temp#0000FF:$_[2] (/dev/$_[0])",
		"GPRINT:temp:MIN:  Min\\: %2.lf",
		"GPRINT:temp:MAX: Max\\: %2.lf",
		"GPRINT:temp:AVERAGE: Avg\\: %4.1lf",
		"GPRINT:temp:LAST: Current\\: %2.lf degrees C\\n";
	if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] graph: $ERROR\n"; }
}
 
Old 11-28-2006, 12:26 PM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
have you actually read the code?

you have multiple subroutine calls:
Code:
# process data for each specified HDD (add/delete as required)
#&ProcessHDD("hda", "primary master");
#&ProcessHDD("hdb", "primary slave");
&ProcessHDD("hdc", "40GB Seagate");
#&ProcessHDD("hdd", "secondary slave");
with a *very* clear comment at the top of it about what they do and how you can change them. if you can successfully run "&ProcessHDD("hdc", "40GB Seagate");" then clearly $_[0] is being used on the inside of that function, so why could you wonder if hda is passed with "&ProcessHDD("hda", "primary master");" ??

from what i can see, nothign is wrong whatsoever, you're simply not reading the script comments.
 
Old 11-28-2006, 01:20 PM   #12
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Sorry im a complete idiot when it comes to this!

The code is perfect but i tried what you said before--to work it out manually.


Code:
my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;
	# remove eol chars and white space
	$temp =~ s/[\n ]//g;

Does

Code:
my $temp=`/usr/local/sbin/hddtemp -n /dev/$_[0]`;

correspond to the following--in the command line?

Code:
/usr/local/sbin/hddtemp -n /dev/hda

What is the "my" for before the variable $temp?
Does the hddtemp correspond to a folder or a command?

Thanks for being so patient
 
Old 11-28-2006, 01:30 PM   #13
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Ive just repeated the whole tutorial agin and this time im getting current value on day graph as 3 degrees?

Its still not the right value but its something different!


Im still gettin no temperature value when i run the perl script.


Code:
meniscus@meniscot:/usr/local/bin$ sudo rrd_hddtemps.pl
Password:
SAMSUNG SP1213N (/dev/hda) temp:  degrees C
 
Old 11-28-2006, 01:41 PM   #14
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
run this:

/usr/local/sbin/hddtemp -n /dev/hda

then run this:

/usr/local/sbin/hddtemp -n /dev/hdc

show us the entire output of both commands.
 
Old 11-28-2006, 02:01 PM   #15
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by meniscus
Also am i right to assume $_[0] $_[1] $_[2] etc...are standard naming conventions for inputs in perl scripting?
When in a subroutine, $_[0], $_[1] ... are the parameters which were used when that sub was called, e.g.

Code:
#!/usr/bin/perl -w

my_subroutine("apples", "pears", "stairs");
exit(0);

sub my_subroutine {
    print '$_[0] is ' . "$_[0]\n";
    print '$_[1] is ' . "$_[1]\n";
    print '$_[2] is ' . "$_[2]\n";
}
...will output:
Code:
$_[0] is apples
$_[1] is pears
$_[2] is stairs
These values are part of the array @_, just like you access the first element of @array with $array[0]. It's a funny looking array name, but that's perl for you! What's interesting about it is that @_ is the default parameter name for lots of functions which expect an array. For example the shift function return the first element of an array, and then remove it from the array, so after:
Code:
my @array = ( "apples", "pears", "stairs" );
my $f1 = shift @array;
...@array will contains ("pears", "stairs"), and $f will contain "apples". Because @_ is the defailt argument to shift, you don't need to specify it where you have @_. This saves a lot of typing, at the expense of some readability for those who don't know perl. So you could re-write my_subroutine above like this:
Code:
sub my_subroutine {
    while ( my $arg = shift ) {
        print "got argument: $arg\n";
    }
}
OK, it's not exactly the same, but you get the idea. The good thing is that it doesn't make any assumptions about the number of arguments passed...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
Send string to std output without interpreting (script) ta0kira Programming 6 06-29-2005 08:50 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 06:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration