#!/bin/bash logv() { if [ "$verbose" == "true" ] then echo "$*" fi } log() { if [ "$silent" == "false" ] then echo "$*" fi } usage() { echo "Usage: getPolledDeviceInfo [-UwvspuaRh] [-t [] [--help]" echo echo " -U Vertica database user (default: dauser)" echo " -w Vertica database password (default: dapass)" echo " -v Enable verbose output" echo " -s Silent. Disable all output except the polled device count." echo " -p List device item IDs that are being polled" echo " -u List device item IDs that are not being polled" echo " -a Include the Primary IP Addresses when listing item IDs with the -p or -u options" echo " -R Consider only poll responses on retired items. (default: consider both retired and unretired)" echo " -t Specifies how far back to check for polled data." echo " polledDataAge is subtracted subtracted from the current" echo " time. If polledDataAge starts with a +, then it is" echo " treated as seconds since epoch. (default: +0)" echo " -r The name of the rate table to check. Can check more than" echo " one by separating with spaces." echo " (default: reach_rate avail_rate physical_memstats_rate nrm_cpustats_rate ifstats_rate)" echo " -T Restricts results to devices in the specified tenant" echo " -h | --help Displays this help message." echo echo echo "Searches the rate tables for polled data with a timestamp greater than the specified polledDataAge. Displays counts for:" echo " Total Devices The total number of devices" echo " Polled Devices The number of devices that have at least one row of polled data in any of the specified rate tables" echo " In other words, this consists of the union of all the devices with polled data in each of the specified rate tables" echo " The number of devices that have at least one row of polled data in this particular rate table" echo echo "It can also display a list of item IDs, and optionally IP addresses, for devices with polled data (or no polled data) in the specified time period." echo echo echo "Examples:" echo echo "Show counts for reachability and availability only:" echo " getPolledDeviceInfo -r reach_rate avail_rate" echo echo "Show device item IDs that don't have polled data for ports in the past hour, and show minimum timestamp (verbose output):" echo " getPolledDeviceInfo -vur ifstats_rate -t 3600" echo echo "Show device item IDs that have polled data for ports in the past hour, and suppress all other output:" echo " getPolledDeviceInfo -spr ifstats_rate -t 3600" echo echo "Show device item IDs that have availability data in the past day, but not in the past hour, include IP addresses, suppress all other output:" echo " getPolledDeviceInfo -spar avail_rate -t 86400 | grep -Fxvf <(getPolledDeviceInfo -spar avail_rate -t 3600)" echo echo "Show device item IDs and IP addresses that have availability data in the past day, but not in the past hour, but have been reachable in the past hour:" echo " getPolledDeviceInfo -par avail_rate -t 86400 | grep -Fxvf <(getPolledDeviceInfo -par avail_rate -t 3600) | grep -Fxf <(/opt/vertica/bin/vsql -U -w -c \"select device.item_id, v6_ntoa( device.primary_ip_address ) from device, item, (select distinct( item_id ) dev_id from reach_rate where im_reachability = 100 and tstamp >= \$(date +%s) - 3600) as foo where item.item_id = device.item_id and device.item_id = foo.dev_id\" | egrep -e '[0-9]' | grep -v row | sed 's/\s//g' | sed 's/|/ /g' | sort -n)" echo echo "Check for any devices where polling has occurred in the past hour for currently retired components:" echo " getPolledDeviceInfo -Rt 3600" exit 0 } mintstamp=+0 maxtstamp= rateTables= defaultRateTables="reach_rate avail_rate physical_memstats_rate nrm_cpustats_rate ifstats_rate" verbose=false silent=false listPolled=false listUnpolled=false includeAddress=false retiredOnly=false tenant= user=dauser pass=dapass mode= for arg in "$@" do case $arg in -*) if [[ "$arg" = *h* ]] then usage else if [[ "$arg" = *U* ]] then mode=user fi if [[ "$arg" = *w* ]] then mode=pass fi if [[ "$arg" = *t* ]] then mode=mintstamp fi if [[ "$arg" = *r* ]] then mode=rateTable fi if [[ "$arg" = *v* ]] then silent=false verbose=true fi if [[ "$arg" = *s* ]] then silent=true verbose=false fi if [[ "$arg" = *p* ]] then listPolled=true fi if [[ "$arg" = *u* ]] then listUnpolled=true fi if [[ "$arg" = *a* ]] then includeAddress=true fi if [[ "$arg" = *R* ]] then retiredOnly=true fi if [[ "$arg" = *T* ]] then mode=tenant fi fi ;; *) case $mode in mintstamp) mintstamp=$arg; mode=maxtstamp;; maxtstamp) maxtstamp=$age;; rateTable) rateTables="$rateTables $arg";; tenant) tenant="$arg";; user) user="$arg";; pass) pass="$arg";; esac ;; esac done #echo "user=$user" #echo "pass=$pass" if [[ $mintstamp == +* ]] then mintstamp=$(echo $mintstamp | sed 's/+//') else mintstamp=$(($(date +%s) - $mintstamp)) fi if [[ $maxtstamp == +* ]] then maxtstamp=$(echo $maxtstamp | sed 's/+//') fi if [ -z "$maxtstamp" ] then tstampClause="and rate.tstamp >= $mintstamp" logv "Considering polled data since $(date -d @$mintstamp) ($mintstamp)" else tstampClause="and rate.tstamp >= $mintstamp and rate.tstamp < $maxtstamp" logv "Considering polled data between $(date -d @$mintstamp) ($mintstamp) and $(date -d @$maxtstamp) ($maxtstamp)" fi if [ "$rateTables" == "" ] then rateTables="$defaultRateTables" fi tenantClause= if [ "$tenant" != "" ] then tenantId="$(/opt/vertica/bin/vsql -U $user -w $pass -c "select item_id from attribute_instance, attribute, qname where qname = '{http://im.ca.com/core}Tenant.Name' and qname_id = attribute_qname_id and attribute.attribute_id = attribute_instance.attribute_id and string_value = '$tenant' limit 1" | egrep -e '[0-9]' | grep -v row | sed 's/\s//g')" if [ -z "$tenantId" ] then echo "Tenant '$tenant' not found" exit 1 fi tenantClause="and item.tenant_item_id = '$tenantId'" fi getPolledDeviceIdsByRateTable() { table=$1 retiredClause= if [ "$retiredOnly" == "true" ] then retiredClause="and exists (select item_id from v_item_facet vif where vif.item_id = pi.item_id and vif.facet_qname like '%Retired')" fi if [ "$includeAddress" == "true" ] then /opt/vertica/bin/vsql -U $user -w $pass -c "select device.item_id, v6_ntoa( device.primary_ip_address ) from device, item, (select distinct( pi.device_item_id ) dev_id from poll_item pi, $table rate where rate.item_id = pi.item_id $tstampClause $retiredClause) as foo where device.item_id = item.item_id and foo.dev_id = device.item_id $tenantClause" 2>/dev/null | egrep -e '[0-9]' | grep -v row | grep -v v6_ntoa | sed 's/\s//g' | sed 's/|/ /g' | sort -n else /opt/vertica/bin/vsql -U $user -w $pass -c "select distinct( pi.device_item_id ) from poll_item pi, item, $table rate where pi.device_item_id = item.item_id and rate.item_id = pi.item_id $tstampClause $retiredClause $tenantClause" 2>/dev/null | egrep -e '[0-9]' | grep -v row | sed 's/\s//g' | sort -n fi } getDeviceIds() { if [ "$includeAddress" == "true" ] then /opt/vertica/bin/vsql -U $user -w $pass -c "select device.item_id, v6_ntoa( device.primary_ip_address ) from device, item where device.item_id = item.item_id $tenantClause" 2>/dev/null | egrep -e '[0-9]' | grep -v row | grep -v v6_ntoa | sed 's/\s//g' | sed 's/|/ /g' | sort -n else /opt/vertica/bin/vsql -U $user -w $pass -c "select device.item_id from device, item where device.item_id = item.item_id $tenantClause" 2>/dev/null | egrep -e '[0-9]' | grep -v row | sed 's/\s//g' | sort -n fi } getPolledDeviceIds() { tmp=$(mktemp) for table in $rateTables do getPolledDeviceIdsByRateTable $table >> $tmp done cat $tmp | sort -n | uniq rm -f $tmp >&/dev/null } if [ "$silent" == "false" ] then echo -n "Total Devices($(getDeviceIds | wc -l | awk '{print $1}'))" echo -n " Polled Devices($(getPolledDeviceIds | wc -l | awk '{print $1}'))" for table in $rateTables do echo -n " $table($(getPolledDeviceIdsByRateTable $table | wc -l | awk '{print $1}'))" done echo elif [ "$listPolled" == "false" ] && [ "$listUnpolled" == "false" ] then echo $(getPolledDeviceIds | wc -l | awk '{print $1}') fi if [ "$listPolled" == "true" ] then log "Polled Devices:" getPolledDeviceIds fi if [ "$listUnpolled" == "true" ] then log "Unpolled Devices:" getDeviceIds | grep -Fvxf <(getPolledDeviceIds) fi