Shell-Script gives in cacti no data back...

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Shell-Script gives in cacti no data back...

Post by WFocht »

Hallo!

I have a little problem with my Script! In the first part i do a cat on a textdocument and set in VAR1-VAR3 the login data for the WMI-Request!
In the second part i do a WMI-Request to a Windows Server and ask him how many Warrnings and Errors are in his Application and System eventlog!

Code: Select all

#!/bin/bash

VAR1=`cat $VAR[0] | head -1 | tail -1 | awk -F '=' '{print $2}'`
VAR2=`cat $VAR[0] | head -2 | tail -1 | awk -F '=' '{print $2}'`
VAR3=`cat $VAR[0] | head -3 | tail -1 | awk -F '=' '{print $2}'`
let VAR4=`wmic -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | grep "|Warnung|" | wc --lines`
let VAR5=`wmic -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | grep "|Warnung|" | wc --lines`
let VAR6=$VAR4+$VAR5
let VAR7=`wmic -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | grep "|Fehler|" | wc --lines`
let VAR8=`wmic -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | grep "|Fehler|" | wc --lines`
let VAR9=$VAR7+$VAR8
echo Fehler:$VAR9 Warnungen:$VAR6

If i set $VAR[0] and $VAR[1] in the Script and run it on the ubuntu i get the right data back but if i get the Script into cacti i always get this in the logfile:
03/30/2011 10:50:23 AM - CMDPHP: Poller[0] Host[9] DS[372] CMD: sh /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh /var/www/cacti/scripts/wmi/wmi_service_login.txt 172.27.27.199, output: Fehler: Warnungen:
Why i get "Fehler: Warnungen:" but no data?!
User avatar
rony
Developer/Forum Admin
Posts: 6022
Joined: Mon Nov 17, 2003 6:35 pm
Location: Michigan, USA
Contact:

Re: Shell-Script gives in cacti no data back...

Post by rony »

You might have to specify the full paths to each binary that you use in the script. This is because you do not have the PATH defined in the environment when cron executes the poller. You can get around this by adding at the top of the crontab file the following:

Code: Select all

PATH=/bin;/usr/bin;/usr/local/bin
[size=117][i][b]Tony Roman[/b][/i][/size]
[size=84][i]Experience is what causes a person to make new mistakes instead of old ones.[/i][/size]
[size=84][i]There are only 3 way to complete a project: Good, Fast or Cheap, pick two.[/i][/size]
[size=84][i]With age comes wisdom, what you choose to do with it determines whether or not you are wise.[/i][/size]
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

Thanks for your answer!

On the top of my Crontab the Path to the binaries is already specified!
hannesd
Posts: 23
Joined: Fri Jun 12, 2009 9:36 am

Re: Shell-Script gives in cacti no data back...

Post by hannesd »

Hi

Did you tried executing this script directly as the "cactiuser"? What are your results in this case? Are all binaries executable (ex. wmic) for the cacti-user?
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

Yes i tried it! I set instent of $VAR[1] the ip adress of the Windows Server and it works! as root and as cactiuser too!
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Shell-Script gives in cacti no data back...

Post by noname »

First, check whether if this command works properly (or not):
# su - cactiuser -c '<your script>'

Next, try this script instead of yours:
(pathname to each commands should be fit to your environment)
#!/bin/bash

CAT=/usr/bin/cat
HEAD=/usr/bin/head
TAIL=/usr/bin/tail
AWK=/usr/bin/awk
WMIC=/usr/local/bin/wmic
GREP=/usr/bin/grep
WC=/usr/bin/wc


VAR1=`$CAT $VAR[0] | $HEAD -1 | $TAIL -1 | $AWK -F '=' '{print $2}'`
VAR2=`$CAT $VAR[0] | $HEAD -2 | $TAIL -1 | $AWK -F '=' '{print $2}'`
VAR3=`$CAT $VAR[0] | $HEAD -3 | $TAIL -1 | $AWK -F '=' '{print $2}'`
let VAR4=`$WMIC -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | $GREP "|Warnung|" | $WC --lines`
let VAR5=`$WMIC -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | $GREP "|Warnung|" | $WC --lines`
let VAR6=$VAR4+$VAR5
let VAR7=`$WMIC -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | $GREP "|Fehler|" | $WC --lines`
let VAR8=`$WMIC -U $VAR1/$VAR2%$VAR3 //$VAR[1] 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | $GREP "|Fehler|" | $WC --lines`
let VAR9=$VAR7+$VAR8
echo Fehler:$VAR9 Warnungen:$VAR6
It is silly test, but if this worked, probably you have some problems with path to commands.
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

Hallo!

Thanks for your help! I've tried this to steps out but the result is the same!

i still get the same information in the cactilog

04/05/2011 06:30:22 PM - CMDPHP: Poller[0] Host[9] DS[370] CMD: sh /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh /var/www/cacti/scripts/wmi/wmi_service_login.txt , output: Fehler: Warnungen:
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Shell-Script gives in cacti no data back...

Post by noname »

WFocht wrote:CMD: sh /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh ...
Your 'sh' is bash?
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

if i run the script like this in the shell i get the right output! Instet of "IP-Address" i set the IP-Address of the Windows Host!

Code: Select all

#!/bin/bash

VAR1=`cat wmi_service_login.txt | head -1 | tail -1 | awk -F '=' '{print $2}'`
VAR2=`cat wmi_service_login.txt | head -2 | tail -1 | awk -F '=' '{print $2}'`
VAR3=`cat wmi_service_login.txt | head -3 | tail -1 | awk -F '=' '{print $2}'`
let VAR4=`wmic -U $VAR1/$VAR2%$VAR3 //IP-Address 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | grep "|Warnung|" | wc --lines`
let VAR5=`wmic -U $VAR1/$VAR2%$VAR3 //IP-Address 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | grep "|Warnung|" | wc --lines`
let VAR6=$VAR4+$VAR5
let VAR7=`wmic -U $VAR1/$VAR2%$VAR3 //IP-Address 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="Application"' | grep "|Fehler|" | wc --lines`
let VAR8=`wmic -U $VAR1/$VAR2%$VAR3 //IP-Address 'SELECT * FROM Win32_NTLogEvent WHERE Logfile ="System"' | grep "|Fehler|" | wc --lines`
let VAR9=$VAR7+$VAR8
echo Warnungen:$VAR6 Fehler:$VAR9
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Shell-Script gives in cacti no data back...

Post by noname »

WFocht wrote:if i run the script like this in the shell i get the right output!
What is the command line you did at that time?
Shell environment for cron is not always same as root user.

And, try this in your Data Input Method:
/bin/bash /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh ...
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

Sry im not a pro in Linux ^^

what do you mean with "What is the command line you did at that time?"?

I use the normal installation of Ubuntu 10.10 server 64 bit!

if i do a /bin/bash instead of sh i get this information in the cactilog...

04/06/2011 11:00:22 AM - CMDPHP: Poller[0] Host[9] DS[370] CMD: /bin/bash /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh /var/www/cacti/scripts/wmi/wmi_service_login.txt , output: Fehler:0 Warnungen:0
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: Shell-Script gives in cacti no data back...

Post by noname »

WFocht wrote:what do you mean with "What is the command line you did at that time?"?
For example,

(A) prompt# sh /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh
(B) prompt# /bin/sh /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh
(C) prompt# /var/www/cacti/scripts/wmi/wmi_eventlog_warnung.sh
(added permission for executable)

If you executed by (A) or (B), and 'sh' is not bash, then '#!/bin/bash' at top of the script has no effect.
Script is performed by other shell, so perhaps some method which depends on bash (such as 'let') will fail.

By the way,
WFocht wrote:output: Fehler:0 Warnungen:0
is this result 'right' for you?
WFocht
Posts: 9
Joined: Fri Jul 16, 2010 7:47 am

Re: Shell-Script gives in cacti no data back...

Post by WFocht »

ahh

yes sh is bash!

and no the result is not right...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest