Discussion:
Telnet data pull example
themactech
2012-10-25 17:32:39 UTC
Permalink
themactech [http://community.zenoss.org/people/themactech] created the discussion

"Telnet data pull example"

To view the discussion, visit: http://community.zenoss.org/message/69520#69520

--------------------------------------------------------------
I have had a couple of people ask me to post how I get data from a device over Telnet into Zenoss.  This is my attempt at explaining this.  I will try to go over how I built my ZenPack to try and cover everything.  If I miss the mark somewhere let me know.

I use the expect language to automate the telnet session so this must be installed on your Zenoss server.

Create a ZenPack where you will store all of this so you can transplant it to other installs.

First off you need to create a monitoring template that you will apply to the device you need to query, name it what you will.

Then in that monitoring template, you will create a COMMAND datapoint.  Mine looks something like this:

${here/ZenPackManager/packs/ZenPacks.<your company>.<your ZenPack>/path}/libexec/./trigger ${dev/zWinUser} ${dev/zWinPassword} ${dev/manageIp} ${here/ZenPackManager/packs/ZenPacks.<your company>.<your ZenPack>/path}

Replace <your company> and <your ZenPack> with your info, i.e. ZenPacks.Microsoft.WindozeTelnet

If the libexec folder does not exist in the root folder of your ZenPack, create it.  The libexec folder is where I put the script, it is not an arbitrary folder, I was told to use this folder by someone smarter than me and since it works I've kept the habit.

So the first portion:

${here/ZenPackManager/packs/ZenPacks.<your company>.<your ZenPack>/path}/libexec/./trigger

     tells which script to run, in this case it's a bash script called 'trigger' in the 'libexec' folder of the ZenPack.

The next 4 items of the command are simply 4 variables we pass to the script, they are in order:
     -dev/zWinUser (since my device is not a windows device I use zWinUser and zWinPassword to hold it's login credentials for telnet)
     -dev/zWinPassword
     -dev/manageIp (the IP address of the device)
     -The path of the ZenPack using ZenPackManager, I need this since I want my text dump to occur within the ZenPack, hence it needs to know where it resides on the file system

Now in the /libexec folder I will have 2 scripts, the bash script called 'trigger' and an expect script called 'pulltelnet'.

This is the start of the bash script:

#!/bin/bash

TheName=$1
ThePass=$2
TheIP=$3
ThePath=$4
TheValid="0"     ** for command script output logic
TheError="0"     ** for command script output logic

TheData=`expect "$ThePath"/libexec/pulltelnet "$TheName" "$ThePass" "$TheIP" | tee "$ThePath"/tests/"$TheIP"`

** the line above will call our expect script, passing it user credentials and IP address, and dump the whole telnet transaction (using tee) to a text file in the 'tests' folder of the ZenPack, the file will be named with the IP address of the device, ensuring no duplicates.

This is what the 'pulltelnet' expect script looks like:

#!/usr/bin/expect

set TheName [lindex $argv 0]
set ThePass [lindex $argv 1]
set TheIP [lindex $argv 2]

spawn telnet $TheIP
sleep 1
expect "Login:"
send "$TheName\r"
expect "Password:"
send "$ThePass\r"
expect "command"
sleep 1
send "alarms\r"
expect "sent"
sleep 1
send "exit\r"

**  This being called from the trigger script will dump all the output to the text file specified above, the rest of the trigger script then continues executing and this is where we parse the data.  Here is what the rest of my 'trigger' script looks like:

TempLow=`grep -a '1........Below' "$ThePath"/tests/"$TheIP" | awk '{ print $4}' | awk -F '.' '{ print $1}'`
TempHigh=`grep -a '1........Above' "$ThePath"/tests/"$TheIP" | awk '{ print $4}' | awk -F '.' '{ print $1}'`
HumLow=`grep -a '2........Below' "$ThePath"/tests/"$TheIP" | awk '{ print $4}' | awk -F '.' '{ print $1}'`
HumHigh=`grep -a '2........Above' "$ThePath"/tests/"$TheIP" | awk '{ print $4}' | awk -F '.' '{ print $1}'`


if [ -z "$TempHigh" ]; then
          TheValid="1"
fi

if [ -z "$TempLow" ]; then
          TheValid="1"
fi

if [ -z "$HumHigh" ]; then
          TheValid="1"
fi

if [ -z "$HumLow" ]; then
          TheValid="1"
fi

if [ "$TheValid" -eq "0" ]; then
     echo "TempHigh $TempHigh" > "$ThePath"/tests/"$TheIP".thresh
     echo "TempLow $TempLow" >> "$ThePath"/tests/"$TheIP".thresh
     echo "HumHigh $HumHigh" >> "$ThePath"/tests/"$TheIP".thresh
     echo "HumLow $HumLow" >> "$ThePath"/tests/"$TheIP".thresh
else
     IsThere=`ls "$ThePath"/tests/ | grep "$TheIP.thresh" | wc -l`
     if [ "$IsThere" -eq "1" ]; then
          IsOld=`find "$ThePath"/tests/ -name "$TheIP".thresh -newerct '23 hour ago' | wc -l`
          TempLow=`grep TempLow "$ThePath"/tests/"$TheIP".thresh | awk '{ print $2 }'`
          TempHigh=`grep TempHigh "$ThePath"/tests/"$TheIP".thresh | awk '{ print $2 }'`
          HumLow=`grep HumLow "$ThePath"/tests/"$TheIP".thresh | awk '{ print $2 }'`
          HumHigh=`grep HumHigh "$ThePath"/tests/"$TheIP".thresh | awk '{ print $@ }'`
          if [ "$IsOld" -eq "0" ]; then
               TheError="2"
          else
               TheError="1"
          fi
     else
          TheError="2"
     fi
fi

TheResult="Datasource|TempHigh="$TempHigh" TempLow="$TempLow" HumHigh="$HumHigh" HumLow="$HumLow" TheError="$TheError
echo $TheResult
exit $TheError

So I have 5 datapoints to my COMMAND, TempHigh, TempLow, HumHigh, HumLow, and TheError.

In a nutshell:

1-     The COMMAND calls a bash script passing it: IP address, User, Password, and Path to ZenPack
2-     The bash script calls an expect script and passes it: IP address, User, Password
3-     The expect script does the telnet transaction and returns the session data to the bash script that called it
4-     The bash script dumps the data to a text file in the ZenPack's folder structure (in the tests folder)
5-     The bash script parse the text file and does some logic to generate the data points (in nagios format) to pipe it into Zenoss

You might need to read up a bit on expect scripts, if I were more proficient at it I might skip the part where I dump to a text file and parse, but I am used to parsing data and not so good at expect so that the way I roll.

Let me know if any info is missing

Manuel
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69520#69520]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Jairo Rodriguez
2012-10-25 19:43:29 UTC
Permalink
Jairo Rodriguez [http://community.zenoss.org/people/jairo] created the discussion

"Re: Telnet data pull example"

To view the discussion, visit: http://community.zenoss.org/message/69538#69538

--------------------------------------------------------------
TheName=$1
ThePass=$2
TheIP=$3
ThePath=$4
TheValid="0"     ** for command script output logic
TheError="0"     ** for command script output logic


In this part we have replace the $1, $2, $3, etc, with the our information.
Ej:

TheName=myuser
ThePass=******** (Where *** is my pass)
TheIP=192.168.1.X
ThePath=(What gonna be the value here?)
TheValid="0"     ** for command script output logic
TheError="0"     ** for command script output logic

I maybe sound dumb but I am new in this matter.

Thank you.

Jairo
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69538#69538]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
themactech
2012-10-25 19:53:31 UTC
Permalink
themactech [http://community.zenoss.org/people/themactech] created the discussion

"Re: Telnet data pull example"

To view the discussion, visit: http://community.zenoss.org/message/69539#69539

--------------------------------------------------------------
This should be called from a COMMAND data source via a monitoring template you have created.  You are skipping steps.  The variable $ThePath should be populated automatically with the path to your ZenPack by Zenoss.

Don't worry about being new to this, I ran into that too.  I will try to help as much as I can.

Have you created ZenPacks before, and have you created monitoring templates?  If not we're start over from the beginning.

Manuel
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/69539#69539]

Start a new discussion in zenoss-users by email
[discussions-community-forums-zenoss--***@community.zenoss.org] -or- at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...