HRULE at MIN-value in RRDTOOL - perl

I'm trying to get a Horizontal line (HRULE) at the lowest value in my RRD-GRAPH with out any good result :(
I have only one data source in the database, end the script is coded in PEARL
#!/usr/bin/perl
use RRDs;
my $cur_time = time();
my $start_time = $cur_time - 86400; # sätt start ill 24 timmar sedan
my $end_1 = time() - 86400;
my $start_1 = $end_1 - 86400;
RRDs::graph "/var/www/ute.png",
"--start= $start_time",
"--end= $cur_time",
"--title= outdoor temperature 1-WIRE 24h",
"--height= 600",
"--width= 1000",
"--vertical-label= °C",
"--upper-limit= 30",
"--lower-limit= -30",
"DEF:Temperatur=/home/nordviken/rrddata/ute_temp.rrd:ute:AVERAGE",
"CDEF:a=Temperatur,-50,0,LIMIT",
"DEF:b=/home/nordviken/rrddata/ute_temp.rrd:ute:AVERAGE:end= $end_1:start= $start_1",
"SHIFT:b:86400",
"COMMENT:\t\t\t\t Nu Medel Max Min\\n",
"HRULE:0#003300",
"LINE1:Temperatur#ff0000:ute\t\t\t\t",
"LINE1:a#0000ff",
"LINE1:b#00ff00",
"GPRINT:Temperatur:LAST:%6.1lf",
"GPRINT:Temperatur:AVERAGE:%6.1lf",
"GPRINT:Temperatur:MAX:%6.1lf",
"GPRINT:Temperatur:MIN:%6.1lf\\n";
my $err=RRDs::error;
if ($err) {print "problem med att skapa grafen: $err\n";}
print "klar!\n"

You could use
VDEF:m=Temperatur,MINIMUM
LINE1:m#003300

Related

text processing to select date range

I have below input and I want to select lines with dates from now to 2 weeks or 3 weeks and so on.
0029L5 08/19/2017 00:57:33
0182L5 08/19/2017 05:53:57
0183L5 02/17/2018 00:00:16
0091L5 10/19/2022 00:00:04
0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
0100L5 08/20/2017 01:25:39
0111L5 08/21/2017 00:46:15
0128L5 08/21/2017 12:38:51
D00054 07/21/2017 09:01:19
So the desired output if let say I want for 2 weeks from now
0045L5 07/27/2017 09:03:56
D00054 07/21/2017 09:01:19
But if i want for let say 4 weeks then the output should be
0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
D00054 07/21/2017 09:01:19
One way:
awk '{split($2,a,"/");split($3,b,":"); x=mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3]);y=systime();}x>y && x<(y+(n*7*24*60*60))' n=2 file
where n indicates the number of weeks
split($2,a,"/") => Split the 2nd column on the basis of / and store in array a
split($3,b,":") => Split the 3rd column on the basis of : and store in array b
mktime => gives the time in seconds
x contains the time in file in seconds
y contains the current time in seconds
Here's one solution using bash where file is the name of your file:
while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s))); echo $(($dd/(3600*24))); done < file
This will compute the date difference in seconds between the date in ${r:6} (substring of the current row) and today's date $(date +%s) and convert it to days.
To output only lines where the date difference is less than 2 weeks (1209600 seconds)
while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s))); if [ "$dd" -lt 1209600 ]; then echo $r; fi; done < file
This works fine, Please let me know in case anybody has any other simpler solution for AIX.
awk '{split($2,a,"/");split($3,b,":"); print $1,b[3],b[2],b[1],a[2],a[1],a[3]}' /tmp/TLD_1 | head -10 | while read media sec min hour day mon year; do month=$((10#$mon-1)); expiry=$(perl -e 'use Time::Local; print timegm(#ARGV[0,1,2,3], $ARGV[4], $ARGV[5]), "\n";' $sec $min $hour $day $month $year); current=$(date +%s); twoweeks=$(($current + (2*7*24*60*60))); if [ "$expiry" -gt "$current" -a "$expiry" -lt "$twoweeks" ]; then echo "$media $mon/$day/$year $hour:$min:$sec"; fi; done

How to include Percentage symbol inside sprintf [duplicate]

This question already has answers here:
How do I print the '%' character with 'printf'?
(6 answers)
Closed 7 years ago.
I would like to ask how can i include the symbol of percentage(%) into the output generated.
Below is my code.
$Formula = ($valueiii / $valuei) * 100 ;
$Formula = sprintf("%04.2f", $Formula);
$Formula = sprintf("%-12.12s", $Formula); # File Name
push (#data2 , " $valueiv$valuei$valueiii$Formula$valuev$valueii\t\n") ;
$Formula = 0;
*Output generate was at the slop of Use(%)*
DevicePart Total Used Use(%) LIMIT(%)
A 50000 10161 20.32 60%
B 100000 1416 1.42 80%
C 125000 42258 33.81 80%
D 5000 18 0.36 60%
i am expecting to get the 20.32% , 1.42% , 33.81% , 0.36% .
But how to included the % into each of the column.
perldoc -f sprintf:
Perl's sprintf permits the following universally-known conversions:
%% a percent sign

Gnuplot prints a strange year, 30 years later

I have the next problem with gnuplot, when I print the time gnuplot
prints de time+30years.
This is a part of my data:
1411336800,1390,0,0,0,10,1411,0,10,0,0,0,0,0,1411
1411340400,1506,0,0,0,10,969,0,10,0,0,0,0,0,969
1411344000,1115,0,0,0,10,1108,0,10,0,0,0,0,0,1108
1411347600,719,0,0,0,10,712,0,10,0,0,0,0,0,712
A part of the script is:
set timefmt "%s"
stats "<tail -1 uur.txt " using 1:2 nooutput
tijd = strftime("%d %B %Y %H:%M", STATS_max_x)
print tijd
And then gnuplot prints: 21 September 2044 01:00. 44 ?
Has some one a clue?
I tried several formats but nothing helped.
Until version 4.6, internally gnuplot uses the 1. January 2000 as reference for its date and time functions (in version 5.0 the standard Unix timestamp is used).
You shouldn't have any problems with set timefmt "%s" if you plot the data. But when using strftime it makes a difference. Since you're using tail anyway, you can simply use
tijd = system('date -d #$(tail -1 uur.txt | cut -d, -f1) +"%d %B %Y %H:%M"')
print tijd

Translating csh switch to perl

I am currently translating some scripts from csh to perl. I have come across one script which has the following switch control
#And now some control
set get_command = h
set finish = 0
while (1)
switch ($get_command)
case "h":
case "H":
set cine_command = ""
cat << EOF
Control synchronised cine by (case insensitive):
A - A view data
B - B view data
a - accelerate
d - decelerate
r - real time heart rate
<num> - rate (frames per second)
i - toggle interpolation
s - step through (may lose a little synchronisation)
c - continue (restart) after stepping
y - reverse direction
h - help (repeat this)
f - finish (quit)
q - quit (finish)
<return> - quit
EOF
breaksw
case "":
case "f":
case "F":
case "q":
case "Q":
set cine_command = '-f'
set finish = 1
breaksw
case "a":
set cine_command = '-a'
breaksw
case "d":
case "D":
set cine_command = '-d'
breaksw
case "r":
case "R":
set cine_command = "-t $time_per_frame"
breaksw
case "i":
case "I":
set cine_command = '-i'
breaksw
case "s":
case "S":
set cine_command = "-s"
breaksw
case "c":
case "C":
set cine_command = "-c"
breaksw
case "y":
case "Y":
set cine_command = "-y"
breaksw
case '[0-9]*':
set cine_command = "-r $get_command"
breaksw
default:
echo "$get_command ignored"
set cine_command = ""
endsw
if ('$cine_command' != '') then
select_tv $FIRST_TV
cine $cine_command
select_tv $SECOND_TV
cine $cine_command
endif
#
# If we're stopping then get out of this loop.
#
if ($finish) break
echo -n "cine > "
set get_command = $<
end
I have Perl 5.8.8 installed on my system and using use Strict;which I know may become deprecated in the next perl release, I have tried the following
#Add some fine control to script
my $get_command = 'h';
my $finish = 0;
my $cine_command;
while(<>)
{
switch ($get_command)
{
case [hH] {$cine_command = "";}
print STDOUT << 'END';
Control synchronised cine by (case insensitive):
A - A view data
B - B view data
a - accelerate
d - decelerate
r - real time heart rate
<num> - rate (frames per second)
i - toggle interpolation
s - step through (may lose a little synchronisation)
c - continue (restart) after stepping
y - reverse direction
h - help (repeat this)
f - finish (quit)
q - quit (finish)
<return> - quit
END
case [fFqQ]
{
$cine_command = '-f';
$finish = 1;
}
case "a"
{
$cine_command = '-a';
}
case [dD]
{
$cine_command = '-d';
}
case [rR]
{
$cine_command = "-t $time_per_frame";
}
case [iI]
{
$cine_command = '-i';
}
case [sS]
{
$cine_command = '-s';
}
case [cC]
{
$cine_command = '-c';
}
case [yY]
{
$cine_command = '-y'
}
case /\d/
{
$cine_command = "-r $get_command";
}
else
{
print "$get_command ignored\n";
$cine_command = "";
}
if ($cine_command ne "")
{
`select_tv $FIRST_TV`;
`cine $cine_command`;
`select_tv $SECOND_TV`;
`cine $cine_command`;
}
exit if( $finish == 1);
print STDOUT "cine > \n";
chomp(my $get_command = <STDIN>);
}
}
When I press return I get the required options printed to the terminals. However, when I type any option into STDIN - e.g a, h or d - I get no response. When I enter retirn - I get the message "h ignored" printed to the terminal as expected.
Any ideas?
To those who say Get a newer version of Perl): Not everyone has control over their systems. If the OP is on Red Hat, it's probably a company machine which means the OP doesn't have control over it. The latest releas of Perl on Redhat is 5.8.8. We might consider it antique and obsolete, but many corporate version of Perl use it.
I have the latest and greatest versions of Perl on my local machines that I control, but I have to write using Perl 5.8.8 syntax, and I have to assume I can't download any CPAN modules.
This is real life, and it can suck. But, you have to live with it...
First off, Perl and Csh are two completely languages and doing a line-by-line translation of any language into another is not a good idea. Perl is a much more powerful language, and it might be nice to use many of the features to improve your csh script.
If you are parsing a command line, look into using the module Getopts::Long which will do a lot of the work for you and is available in Perl 5.8.8. This may do exactly what you're trying to do with the switch statement and take a lot less work and is a lot more flexible.
I would avoid using the switch statement in Perl. It never really worked very well. The new given/when stuff works better, but alas, it's only available since Perl 5.10.
Instead, forget the switch stuff and use an if/elsif structure then case statement:
my $usage =<<USAGE;
Control synchronised cine by (case insensitive):
A - A view data
B - B view data
a - accelerate
d - decelerate
r - real time heart rate
<num> - rate (frames per second)
i - toggle interpolation
s - step through (may lose a little synchronisation)
c - continue (restart) after stepping
y - reverse direction
h - help (repeat this)
f - finish (quit)
q - quit (finish)
<return> - quit
USAGE
$cine_command;
$finish;
while ( my $command = get_command() ) {
if ( $command =~ /^h$/i ) {
print "$usage\n";
exit 0;
}
elsif ( $command =~ /^(fq)$/i ) {
$cine_command = '-f'
$finish = 1
}
elsif ( $command =~ /^a$/i ) {
$cine_command = '-a';
}
elsif ...
It's not as elegant as a switch statement, but it does the job and is easy to understand and maintain. Also take advantage of using regular expression matching. For example $command =~ /^(fq)$/i is checking to see if$commandis equal toF,f,q, orQ` all at the same time.
while(<>){} in perl is not equivalent to while(1)in csh. while(<>) in perl is equivalent to while(defined($_ = <>)) which is a read and a check to see that eof has not been reached. try replacing while(<>) with while(1).
You might look at something like App::Cmd for a larger program with lots of options. Otherwise, a hash of subrefs might work for delegation rather than a switch statement.

pyephem: can't calculate sunrise/set for polar regions

i'm trying to calculate sunrises and sunsets using pyephem, but the algorithm never seems to converge for polar regions?
observe the sample code below. it iterates through an entire year in 10-minute increments asking for the next sunrise and sunset. pyephem always returns with an AlwaysUpError or NeverUpError, but surely the sun must rise and set at least once during the year?
import ephem
from datetime import datetime, timedelta
obs = ephem.Observer()
obs.lat = '89:30'
obs.long = '0'
start = datetime(2011, 1, 1)
end = datetime(2012, 1, 1)
step = timedelta(minutes=10)
sun = ephem.Sun()
timestamp = start
while timestamp < end:
obs.date = timestamp
try:
print obs.next_rising(sun)
except (ephem.AlwaysUpError, ephem.NeverUpError):
pass
try:
print obs.next_setting(sun)
except (ephem.AlwaysUpError, ephem.NeverUpError):
pass
try:
print obs.previous_rising(sun)
except (ephem.AlwaysUpError, ephem.NeverUpError):
pass
try:
print obs.previous_setting(sun)
except (ephem.AlwaysUpError, ephem.NeverUpError):
pass
timestamp += step
either i'm using the api incorrectly, there's a bug in pyephem, or i'm misunderstanding something fundamental. any help?
I suspect some sort of improper caching. Consider:
import ephem
atlanta = ephem.Observer()
atlanta.pressure = 0
atlanta.horizon = '-0:34'
atlanta.lat, atlanta.lon = '89:30', '0'
atlanta.date = '2011/03/18 12:00'
print atlanta.previous_rising(ephem.Sun())
print atlanta.next_setting(ephem.Sun())
atlanta.date = '2011/03/19 12:00'
print atlanta.previous_rising(ephem.Sun())
print atlanta.next_setting(ephem.Sun())
atlanta.date = '2011/03/20 12:00'
print atlanta.previous_rising(ephem.Sun())
# print atlanta.next_setting(ephem.Sun())
atlanta.date = '2011/09/24 12:00'
# print atlanta.previous_rising(ephem.Sun())
print atlanta.next_setting(ephem.Sun())
atlanta.date = '2011/09/25 12:00'
print atlanta.previous_rising(ephem.Sun())
print atlanta.next_setting(ephem.Sun())
atlanta.date = '2011/09/26 12:00'
print atlanta.previous_rising(ephem.Sun())
print atlanta.next_setting(ephem.Sun())
which yields:
2011/3/18 07:49:34
2011/3/18 17:44:50
2011/3/19 05:04:49
2011/3/19 21:49:23
2011/3/20 01:26:02
2011/9/24 19:59:09
2011/9/25 04:57:21
2011/9/25 17:14:10
2011/9/26 08:37:25
2011/9/26 14:03:20
which matches to the minute with USNO results:
https://raw.github.com/barrycarter/bcapps/master/db/srss-895.txt
See also my related whiny complain in linked question.
I just ran your program and got this output (piped to "sort | uniq -c"):
260 2011/3/17 11:32:31
469 2011/3/17 13:42:56
184 2011/3/18 07:25:56
350 2011/3/18 18:13:15
191 2011/3/19 04:41:42
346 2011/9/24 20:25:13
337 2011/9/25 04:27:45
214 2011/9/25 17:36:10
166 2011/9/26 08:00:59
254 2011/9/26 14:37:06
Are you sure you have the indentations right? Here's my raw code:
https://raw.github.com/barrycarter/bcapps/master/playground4.py
(the output doesn't match my other answer above, but we're using different horizons (-34 minutes vs -50 minutes).
i've found using the start parameter to obs.next_rising(), etc., yield better results. however it still sometimes seems to miss certain crossings; the rises it finds don't always pair off with a corresponding set.