LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Feeding data from shell script in Graphite Graph (https://www.linuxquestions.org/questions/linux-software-2/feeding-data-from-shell-script-in-graphite-graph-902314/)

niaz_ph 09-10-2011 12:40 PM

Feeding data from shell script in Graphite Graph
 
Hi,

I am trying to feed data from shell script in Graphite real time graph software.

i've written a simple script::

========================================================
#!/bin/bash
date=`date +%s`
hostname=`hostname --short`
server="192.168.6.240"
port=2003
ping -c 5 www.google.com | cut -d ":" -f2 > ping_result.txt
grep "icmp" ping_result.txt |cut -d " " -f4 |cut -d "=" -f2 > ping_resut.txt
a=`cat ping_result.txt`
nc -v -c "echo \"carbon.$hostname.ping.google.latency $a $date\"" $server $port ;

========================================================

my graphite server ip is 192.168.6.240.
this code is for pinging google and the output will be the RTA time. but whenever i execute it then i find no graph on graphite. Wht's the wrong with it ?

David the H. 09-10-2011 02:10 PM

First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

You need to give us some more details here. What exactly is the output that the script supposed to produce, and what does it actually produce? I'm not familiar with graphite, and the command doesn't even appear in your script, so I'm assuming that it's meant only to produce output in a format that graphite can read. So does it?

I don't know much about nc either, but this line still doesn't look quite right to me. Is this how the command syntax supposed to look? The manpage for my version of netcat doesn't even mention a -c option; and do you really want to send it the literal "echo" string here?

Code:

nc -v -c "echo \"carbon.$hostname.ping.google.latency $a $date\"" $server $port ;
In any case, you have to do some legwork. Break the process down step-by-step and make sure it works at every point. echo your variables and commands, and see if each command is doing what you want it to do.

See here for more on script debugging:
http://mywiki.wooledge.org/BashGuide...ices#Debugging

Also, these two lines are quite wasteful:

Code:

ping -c 5 www.google.com | cut -d ":" -f2 > ping_result.txt
grep "icmp" ping_result.txt |cut -d " " -f4 |cut -d "=" -f2 > ping_resut.txt

That's a lot of work just to get the time string from the output of ping, when you can use a single sed or awk command to do the same thing. I'd also recommend storing the output in an array variable, rather than a file, unless you have some additional reason for storing it externally.

Something like this:
Code:

date="$( date +%s )"
server="192.168.6.240"
port=2003
ping_result=( $( ping -c 5 www.google.com | awk -F '[ =]' '/icmp/ { print $11 }' ) )
nc -v -c "echo \"carbon.$HOSTNAME.ping.google.latency ${ping_result[*]} $date\"" $server $port ;

Bash also sets a $HOSTNAME shell variable, so you can probably skip the hostname command as well.

Notice also that $(..) is highly recommended over `..`

niaz_ph 09-11-2011 01:01 AM

Quote:

Originally Posted by David the H. (Post 4468314)
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

You need to give us some more details here. What exactly is the output that the script supposed to produce, and what does it actually produce? I'm not familiar with graphite, and the command doesn't even appear in your script, so I'm assuming that it's meant only to produce output in a format that graphite can read. So does it?

I don't know much about nc either, but this line still doesn't look quite right to me. Is this how the command syntax supposed to look? The manpage for my version of netcat doesn't even mention a -c option; and do you really want to send it the literal "echo" string here?

Code:

nc -v -c "echo \"carbon.$hostname.ping.google.latency $a $date\"" $server $port ;
In any case, you have to do some legwork. Break the process down step-by-step and make sure it works at every point. echo your variables and commands, and see if each command is doing what you want it to do.

See here for more on script debugging:
http://mywiki.wooledge.org/BashGuide...ices#Debugging

Also, these two lines are quite wasteful:

Code:

ping -c 5 www.google.com | cut -d ":" -f2 > ping_result.txt
grep "icmp" ping_result.txt |cut -d " " -f4 |cut -d "=" -f2 > ping_resut.txt

That's a lot of work just to get the time string from the output of ping, when you can use a single sed or awk command to do the same thing. I'd also recommend storing the output in an array variable, rather than a file, unless you have some additional reason for storing it externally.

Something like this:
Code:

date="$( date +%s )"
server="192.168.6.240"
port=2003
ping_result=( $( ping -c 5 www.google.com | awk -F '[ =]' '/icmp/ { print $11 }' ) )
nc -v -c "echo \"carbon.$HOSTNAME.ping.google.latency ${ping_result[*]} $date\"" $server $port ;

Bash also sets a $HOSTNAME shell variable, so you can probably skip the hostname command as well.

Notice also that $(..) is highly recommended over `..`

HI,
THanks Thanks and thanks a lot...I dont know wht happened but after i edit scritp according ur advise...it works!!!!...Gr8 Help .....Thnks again....

markseger 01-07-2012 07:52 AM

I've been recently looking for graphite users. I've recently added support to collectl for allowing it to directly send anything it collectl to graphite but have not yet released it. Only thing is I'd like to get some people to try it out before I ship it. Anyone interested?
-mark


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