LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 12-04-2016, 02:10 PM   #1
Olek
Member
 
Registered: Jul 2012
Location: Wroclaw Poland
Distribution: Slackware
Posts: 110

Rep: Reputation: 27
Are regular expressions work correct in Slackware 14.2 and current? Cacti problem


After upgrade from Slack 14.1 to 14.2 my Cacti stopped to show load average.
It's because perl script loadavg.pl, which before produced correct data, now put one stupid comma in output. I don't now why?

Should be
Code:
0.20 0.15 0.14
but script output is
Code:
0.20 0.15, 0.14
Cacti don't like this comma and says:
Code:
Result from CMD not valid.  Partial Result: 0.20 0.15, 0.14
Can You check script below on Yours systems?
And how to fix it?

My loadavg.pl (in original script there isn't "LC_ALL=C" but without it I get more commas)
Code:
#!/usr/bin/perl

#get load avg for 5;15;30 min
#
open(PROCESS,"env LC_ALL=C uptime |");
$avg = <PROCESS>;
$avg =~ s/.*:\s*//;
close(PROCESS);

if ($ARGV[0] eq "5") {
        $avg = `echo "$avg" | awk '\{print \$1 \}'`;
}

if ($ARGV[0] eq "15") {
        $avg = `echo "$avg" | awk '\{print \$2 \}'`;
}

if ($ARGV[0] eq "30") {
        $avg = `echo "$avg" | awk '\{print \$3 \}'`;
}

chomp $avg;
$avg =~ s/,//;
$avg =~ s/\n//;
print $avg;
 
Old 12-04-2016, 02:16 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
See man perlre for the details on regular expressions and the s/// substitution operator, and man perlop as well. What you need is a "g" in the third to last line.

Also, "perl" is far more powerful than "awk" so you can safely replace the calls to "awk" with native perl.

Last edited by Turbocapitalist; 12-04-2016 at 02:17 PM.
 
Old 12-04-2016, 03:17 PM   #3
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Olek --

As far as I know, uptime has always produced output like this:
Code:
 15:10:47 up 2 days,  9:03, 23 users,  load average: 0.16, 0.15, 0.16
man uptime ?

What is it you need to 'feed' Cacti ?

-- kjh

Code:
#
# Slackware 13.37:
#
[konrad@kjhlt5 ~]$ uptime
 15:11:37 up 9 days,  8:56,  2 users,  load average: 0.12, 0.10, 0.13
#
# Slackware 14.2:
#
[konrad@kjhlt6 ~]$ uptime
 15:11:52 up 2 days,  9:04, 23 users,  load average: 0.11, 0.13, 0.15
#
# CentOS 6.x
#
[konrad@broke ~]$ uptime
 15:14:08 up 9 days, 19:23,  1 user,  load average: 0.02, 0.02, 0.00
#
# CentOS 5.x
#
[konrad@pp310 ~]$ uptime
 15:13:22 up 237 days, 23:52, 14 users,  load average: 0.33, 0.15, 0.19
#
# CentOS 4.x
#
[konrad@sog ~]$ uptime
 15:14:54 up 18 days,  1:12,  1 user,  load average: 0.03, 0.09, 0.04
 
Old 12-04-2016, 03:32 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,376

Rep: Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756
Quote:
Also, "perl" is far more powerful than "awk" so you can safely replace the calls to "awk" with native perl.
Personally, I would drop the "perl", as "awk" can cope.
Code:
#!/bin/sh

(( $1 == 5 )) && uptime | awk '{print gensub(",","","g",$(NF-2))}'
(( $1 == 15 )) && uptime | awk '{print gensub(",","","g",$(NF-1))}'
(( $1 == 30 )) && uptime | awk '{print $NF}'

Last edited by allend; 12-04-2016 at 03:49 PM. Reason: Make more robust for when uptime also reports days
 
2 members found this post helpful.
Old 12-04-2016, 03:57 PM   #5
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Thanks allend

I am an `awk guy` myself unless I need to handle binary data

This would also work and I believe it handles the case where not( $1 in 5,15,30 ) the same as Olek's script ( it dumps all-three times ).

Once again though, uptime returns load average for 1, 5 and 15 min and not 5, 15 and 30 min ...

Try man uptime

EDIT: oops ... I meant to say that Turbocapitalist is correct, Olek's substitution needs a trailing 'g' for global: $avg =~ s/,//g;

-- kjh

Code:
#!/bin/sh

uptime |\
gawk '
BEGIN {

   Arg = "'"$1"'"  +0

   OFS = FS = " "

   getline

   gsub( /,/, "", $0 )

   if ( Arg == 5 )
   {
      print $(NF-2)
      exit( 0 )
   }
   if ( Arg == 15 )
   {
      print $(NF-1)
      exit( 0 )
   }
   if ( Arg == 30 )
   {
      print $(NF)
      exit( 0 )
   }
   print $(NF-2),$(NF-1),$(NF)
   exit( 1 )
}'

Last edited by kjhambrick; 12-04-2016 at 04:11 PM.
 
Old 12-04-2016, 04:12 PM   #6
Olek
Member
 
Registered: Jul 2012
Location: Wroclaw Poland
Distribution: Slackware
Posts: 110

Original Poster
Rep: Reputation: 27
Thank You for help. I try to study man perl, but a don't now Perl at all.
Or maybe I'll use this perlless script

And please note:
1. The "loadavg.pl" script was delivered by Cacti package from SBo, so now will make problems not only for me, I thing.
2. This script work fine on Slackware 14.1 so what changed in Slackware 14.2 ?

Olek
 
Old 12-04-2016, 04:43 PM   #7
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Thanks for the info Olek.

I build the cacti Package from SBo and I found: var/www/htdocs/cacti/scripts/loadavg.pl in the resulting Package.

The Code provided by the Cacti Devs in loadavg.pl is broken.

I am not sure why that same script worked on Slackware 14.1 ... there was a major perl update but as Turbocapitalist said, $avg =~ s/,//; will only remove the first comma ( it has been that way since perl 4 ).

In addition, your LC_ALL=C patch is probably REQUIRED these days because some LOCALES use commas instead of dots for the decimal char.

The LC_ALL=C Environment will assure that the decimals are dots.

I would say that the Cacti Devs need to patch their loadavg.pl script.

Being a coder myself, I am hesitant to criticize code written by other coders but here goes ...

As Turbocapitalist said, there is no need to invoke gawk inside a perl script.

As allend said the same thing can be done without perl at all.

IMO, the loadavg.pl script is VERY primative in that it invokes gawk with back-ticks in a perl script to do something that perl capable of doing very well all by itself ( what Turbocapitalist said ).

While that particular script shouldn't be dangerous security-wise, it is however VERY poorly designed ( Larry Wall might have a Camel if he saw it )

I am not sure what other plugins are provided by the Cacti Devs, but given what I see in loadavg.pl, I would tend to be careful deploying their stuff on internet-facing web servers without auditing their code.

Anyhow, my $0.02 ...

-- kjh
 
1 members found this post helpful.
Old 12-04-2016, 05:12 PM   #8
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
On more Olek ...

Since this is a plugin for a php app, you may need a pure-d perl script ( except that the perl script invokes uptime via popen() ).

Below is one that doesn't use gawk but it does invoke uptime via backticks instead of opening a pipe.

There are probably better ways to do it but this one does do what you were looking for in your first post.

HTH.

-- kjh

Code:
#!/usr/bin/perl
# 
# get load avg for 5;15;30 min
#
my $avg = `LC_ALL=C uptime` ;

chomp( $avg ) ;

$avg =~ s/,//g ;

my @fields = split( / /, $avg ) ;

if ( $ARGV[0] eq "5" )
{
   print $fields[ $#fields -2 ] . "\n" ;
}
elsif ( $ARGV[0] eq "15" )
{
   print $fields[ $#fields -1 ] . "\n" ;
}
elsif ($ARGV[0] eq "30" )
{
   print $fields[ $#fields ] . "\n" ;
}
else
{
   print $fields[ $#fields -2 ] 
       . ' ' 
       . $fields[ $#fields -1 ] 
       . ' ' 
       . $fields[ $#fields ] 
       . "\n" ;
}
exit( 0 );
 
Old 12-05-2016, 04:29 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,904

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
You can save yourself a lot of trouble by just reading the data from /proc/loadavg rather than trying to capture/parse the output of uptime.
 
Old 12-05-2016, 04:29 AM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,904

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
You can save yourself a lot of trouble by just reading the data from /proc/loadavg rather than trying to capture/parse the output of uptime.

edit: oops, duplicate post. No idea how that happened.

Last edited by GazL; 12-05-2016 at 04:31 AM.
 
1 members found this post helpful.
Old 12-05-2016, 05:41 AM   #11
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Interesting idea GazL.

Especially if the output of the `uptime` is a non-portable.

Same load numbers on this run ( and there are no commas )

Code:
# cat /proc/loadavg ; uptime

0.24 0.18 0.12 1/618 17927
 05:29:22 up 2 days, 23:22, 23 users,  load average: 0.15, 0.14, 0.10
OTOH, if uptime is portable across 'NIX OS then I imagine the Cacti guys would opt for the portable approach.

Out of curiosity, I did dig around a bit in the Cacti var/www/htdocs/cacti/scripts/ directory.

There are {.sh,.pl,.php} command line scripts in there.

In addition, there is one script: var/www/htdocs/cacti/scripts/loadavg_multi.pl that is more 'perlish' than loadavg.pl
Code:
#!/usr/bin/perl

#get load avg for 1;5;10 min
open(PROCESS, "uptime |");
$avg = <PROCESS>;
close(PROCESS);

#   9:36pm  up 15 days, 11:37,  2 users,  load average: 0.14, 0.13, 0.10

$avg =~ s/^.*:\s(\d+\.\d{2}),?\s(\d+\.\d{2}),?\s(\d+\.\d{2})$//;

print "1min:$1 5min:$2 10min:$3";
The output is different than loadavg.pl but it is a pure-d perl script ( no `gawk` backtick commands ) and the $avg =~ RegEX accounts for optional commas between the 1,5,15 load avg numbers.

Anyhow ... Thanks again.

-- kjh
 
Old 12-10-2016, 04:37 PM   #12
Olek
Member
 
Registered: Jul 2012
Location: Wroclaw Poland
Distribution: Slackware
Posts: 110

Original Poster
Rep: Reputation: 27
Finally i found that now, with cacti-0.8.8h the only way is to use loadavg_multi.pl script.
It's funny that on Slackware 14.1 with cacti-0.8.81_p1 loadavg.pl script worked fine.
Now, on clean new install of Cacti loadavg_multi.pl script is used by default.
All because I upgraded my system instead of building everything from scratch
I guess so current Cacti not accept output of loadavg.pl script any more.

Thanks for Your help.

Olek
 
1 members found this post helpful.
Old 12-11-2016, 02:04 PM   #13
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Thanks for the update Olek.

One thing you might do for posterity is to mark your Thread as SOLVED.

As the O.P. Only you can do that.

And should someone else have the same issue, that SOLVED Tag is good to add to your LQ Searches.

Thanks again.

-- kjh
 
Old 12-12-2016, 02:51 AM   #14
Olek
Member
 
Registered: Jul 2012
Location: Wroclaw Poland
Distribution: Slackware
Posts: 110

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by kjhambrick View Post

One thing you might do for posterity is to mark your Thread as SOLVED.
But I alredy marked thread as SOLVED.
Isn't its status visible?

Olek
 
Old 12-12-2016, 02:37 PM   #15
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
I missed it before but I see it now, Olek

Thanks and sorry about causing confusion.

-- kjh
 
  


Reply



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
[SOLVED] I do not have sound as regular user (Slackware-current) Didier Spaier Slackware 10 05-11-2016 04:15 PM
vbox 4.1.10, slackware current 64, kde not starting for regular user glorsplitz Slackware 1 03-27-2012 07:55 PM
help with regular expressions mariogarcia Linux - Software 3 01-28-2009 03:23 AM
Regular Expressions problem E-Oreo Programming 9 04-09-2004 11:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:34 PM.

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
Open Source Consulting | Domain Registration