LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-29-2006, 03:35 AM   #16
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15

Code:
meniscus@meniscot:~$ /usr/local/sbin/hddtemp -n /dev/hda
bash: /usr/local/sbin/hddtemp: No such file or directory
meniscus@meniscot:~$ /usr/local/sbin/hddtemp -n /dev/hdc
bash: /usr/local/sbin/hddtemp: No such file or directory
meniscus@meniscot:~$
meniscus@meniscot:~$
meniscus@meniscot:~$
meniscus@meniscot:~$ cd /usr/local/sbin/
meniscus@meniscot:/usr/local/sbin$ sudo hddtemp -n /dev/hda
Password:
31
meniscus@meniscot:/usr/local/sbin$ sudo hddtemp -n /dev/hdc
/dev/hdc: TSSTcorpCD/DVDW TS-H552U: drive is sleeping
meniscus@meniscot:/usr/local/sbin$
Sorry for so long in replying. I had a soccer game ls night.

Here is the output that you wanted

Is it graphing the the drive that is sleeping? Is that it?
 
Old 11-29-2006, 08:46 AM   #17
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Finally! After 2 days of hard slog got it working!

hddtemp was in /usr/sbin/ on my system, not /usr/local/sbin/ (the way it was accessed on his script!)

Thanks for all your help anyway
 
Old 11-29-2006, 08:49 AM   #18
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Getting a number from a line of text?

Im trying to write a command thatll return just the CPU temperature

Ive got this so far

Code:
meniscus@meniscot:~$ sensors | grep 'CPU Temp'
CPU Temp:    +43C  (low  =   +15C, high =   +45C)   sensor = diode

How do i modify the line to get just the number 43?
 
Old 11-29-2006, 09:11 AM   #19
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Perl'll do it. I'm sure you'll get awk, sed and python examples too
Code:
sensors | perl -ne 'if ( /CPU Temp:\s*\+(\d+)/ ) { print $1 . "\n"; }'
 
Old 11-29-2006, 09:47 AM   #20
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thank you. i got it this way 2.

Code:
meniscus@meniscot:~$ sensors | grep 'CPU Temp' | head -c 16 | tail -c 2
42meniscus@meniscot:~$
If i assigned this line to a variable in a perl script would it work?

ie

Code:
#Getting temperature and assign to variable my $temp
my $temp=`/usr/bin/sensors | grep 'CPU Temp' | head -c 16 | tail -c 2`;
	
# remove eol chars and white space
	$temp =~ s/[\n ]//g;
Would that work ok?
 
Old 11-29-2006, 10:03 AM   #21
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
Thank you. i got it this way 2.

Code:
meniscus@meniscot:~$ sensors | grep 'CPU Temp' | head -c 16 | tail -c 2
42meniscus@meniscot:~$
Be careful about exact number of characters... you don't want to chop off a digit if it goes over 100. Having said that, if it goes over 100, you've probably got more to worry about that your script

Quote:
If i assigned this line to a variable in a perl script would it work?

ie

Code:
#Getting temperature and assign to variable my $temp
my $temp=`/usr/bin/sensors | grep 'CPU Temp' | head -c 16 | tail -c 2`;
	
# remove eol chars and white space
	$temp =~ s/[\n ]//g;
Would that work ok?
Try it and see. More generally though, I'd urge you to do the pattern matching inside perl rather than spawning extra processes such as grep and head and tail. Spawning a process is much heavier on the machine than doing the pattern matching inside perl. I'd probably approach it like this:
Code:
my $output = `sensors`;
my $temp;
if ( $output =~ /CPU Temp:\s*\+(\d+)/ ) {
    $temp = $1;
}

if ( defined($temp) ) {
    print "temp is $temp\n";
}
else {
    print "can't find temperature!\n";
}
This way you spawn only 1 external process - much cheaper and tidier.

Last edited by matthewg42; 11-29-2006 at 10:04 AM.
 
Old 11-29-2006, 11:00 AM   #22
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks for those tips.

I put your code into a script i have which graphs the Harddrive temperature over time


The oringinal script was this

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





My altered script with your code to access the CPU temp the following


Code:
#!/usr/bin/perl

# Perl script for graphing CPU temperature
# copyright Martin Pot 2003
# http://martybugs.net/linux/hddtemp.cgi
#
# rrd_cputemp.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 CPU temperature
&ProcessCPU("CPU temp", "Intel Prescott");


sub ProcessCPU
{
# process HDD
# inputs: $_[0]: processor temperature
#         $_[1]: processor description

	# get hdd temp for master drive on secondary IDE channel
	#my $temp=`/usr/bin/sensors | grep 'CPU Temp' | head -c 16 | tail -c 2`;
	# remove eol chars and white space
	#$temp =~ s/[\n ]//g;



	my $output = `sensors`;
	my $temp;
	if ( $output =~ /CPU Temp:\s*\+(\d+)/ ) {
    	$temp = $1;
	}
	
	print "$_[1] ($_[0]) temp: $temp degrees C\n";

	# if rrdtool database doesn't exist, create it
	if (! -e "$rrd/$_[0].rrd")
	{
		print "creating rrd database for /$_[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]: CPU temp
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: CPU description

	RRDs::graph "$img/$_[0]-$_[1].png",
		"--lazy",
		"-s -1$_[1]",
		"-t CPU temperature :: $_[2] (/$_[0])",
		"-h", "80", "-w", "600",
		"-a", "PNG",
		"-v degrees C",
		"DEF:temp=$rrd/$_[0].rrd:temp:AVERAGE",
		"LINE2:temp#0000FF:$_[2] (/$_[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"; }
}


im getting it to create the rrd database and print graphs but all the values are being returned as nan!

Is your code used in the right context here?
 
Old 11-29-2006, 11:12 AM   #23
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
The code is, but the $temp variable is only defined within sub ProcessCPU;
I urge you to have the first line of your scripts modified to use
#!/usr/bin/perl -w

That would have told you what's happening ;}



Cheers,
Tink
 
Old 11-29-2006, 11:17 AM   #24
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I merged your two threads on the same subject; for future
reference: please stick with one thread per topic in one forum
to keep discussions in one place.



Cheers,
Tink
 
Old 11-29-2006, 11:30 AM   #25
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Sorry! i wont do it again

I changed the first line from

#!/usr/bin/perl


to

#!/usr/bin/perl -w


But it still giving me nan degrees in the temperature outputs on the graph.


Why does the graph work for the hardrive since the temp variable is defined within the process there as well?
 
Old 11-29-2006, 11:50 AM   #26
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Consider using the -w option whenever you can - it'll issue warnings in helpful places

Also, consider adding "use strict;" which further locks down your script so that some ambiguous syntax can't be used. Again, this does constrain you a little, but it make catching errors a lot easier:
Code:
#!/usr/bin/perl -w

use strict;

...
 
Old 11-29-2006, 11:56 AM   #27
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
I ran it again and it worked! Just cant understand! Its just me an computers!

Thanks a million for all yere help lads. Ye are trully wizkids at this stuff!

Thanks again
meniscus
 
Old 11-29-2006, 12:14 PM   #28
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Just 1 more question

Im finally tryin to graph motherboard temp


meniscus@meniscot:~$ sensors
it8712-isa-0290
Adapter: ISA adapter
M/B Temp: +37C (low = +15C, high = +40C) sensor = thermistor
CPU Temp: +43C (low = +15C, high = +45C) sensor = diode



How do i modify this line to get the motherboard temperature from lm-sensors

Code:
sensors | perl -ne 'if ( /CPU Temp:\s*\+(\d+)/ ) { print $1 . "\n"; }'

It seems to be disagreeing with the "/" in "M/B"

This is the error im getting.

Code:
meniscus@meniscot:~$ sensors | perl -ne 'if ( /M/B Temp:\s*\+(\d+)/ ) { print $1 . "\n"; }'
Bareword found where operator expected at -e line 1, near "/M/B"
        (Missing operator before B?)
syntax error at -e line 1, near "/M/B Temp"
Substitution pattern not terminated at -e line 1.
 
Old 11-29-2006, 12:49 PM   #29
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
in the code above, / is used as a sort of quote to show where the regular expression (pattern) starts and ends. You have several options:
1. Put a \ in front of any / in the pattern to "escape" it (change it from being a special character to being treated like a literal character), for example:
Code:
perl -ne 'if ( /M\/B Temp:\s*\+(\d+)/ ) { print $1 . "\n"; }'
2. Use a different regex match quote. When perl sees
Code:
/something/
, it tries to match the pattern something. This is actually a shorthand for
Code:
m/something/
. Furthermore, the m operator can be used with different de-limiters, e.g. these do the same thing:
Code:
m/pattern/
m|pattern|
The difference being that in the first one, if you want to use a / in the pattern itself, you have to escape it with \ backslash, but in the second you don't. These are the same:
Code:
m/M\/B Temp/
m|M/B Temp|
In short, you could change the code to:
Code:
perl -ne 'if ( m|M\/B Temp:\s*\+(\d+)| ) { print $1 . "\n"; }'
 
Old 11-30-2006, 04:29 AM   #30
meniscus
Member
 
Registered: May 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Alright i pasted the following into crontab last night to get a graph of temperature similiar to the Harddrive one

Code:
# get CPU temperatures
*/10 * * * * root /usr/local/bin/rrd_cputemp.pl > /dev/null
I left it for the night>>came back this morning and the rdd_cputemp.pl(the 1 modified from rrd_hddtemp.pl)didnt keep running!

It seems like it graphs it when i run the script on its own "once" but doesnt continue running!

Its something stupid most likely! Is the crontab bit right as least?
 
  


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 08:42 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration