Adding bulks of SNMP hosts to cacti, updated

I did some changes to the makefile I published last month. It would be too much to completely rewrite the blog post to adapt for the changes, so I am just publishing the update here, and you can do the diff yourself.

There is still a lot of factorization left to do, I am leaving it to when I'll have more time. If, in the meanwhile, you come up with a better version, please leave a comment here.

I still have problems with LVS metrics, in particular: "LVS traffic in" is not getting any data. If you can spot the mistake and tell me, that would be much appreciated.

Have fun! …

########################################################################
#
# This makefile tries to help you to automate the following tasks:
#
# - add an SNMP host to cacti
# - add a set of graphs to the host
# - add the host to a tree
#
# Before using this makefile, please check that the following settings
# are correct for your installation.
#
########################################################################
PHP=/usr/bin/php
CLI_DIR=/opt/cacti/cli

# Default tree where we add hosts to
DEFTREE_ID=1

# Default host template to use
HOST_TID=9

# Host template to use for LVS
LVS_HOST_TID=10

# In the following variables, you have:
# *_GTID:       Graph Template ID
# *_QID:        Query ID
# *_QTID:       Query template ID

# Network interfaces
IFACE_GTID=2
IFACE_QID=1
IFACE_QTID=13
IFACE_FIELD=ifOperStatus
IFACE_VALUE=Up

# Individual CPUs
CPUS_GTID=27
CPUS_QID=9
CPUS_QTID=19
CPUS_FIELD=hrProcessorFrwID

# Cumulative CPU info
CPU_GTID=38

# Load average
LA_GTID=11

# Memory
MEM_GTID=39

# Processes
PROC_GTID=37

# Used space
US_GTID=26
US_QID=8
US_QTID=18
US_FIELD=hrStorageDescr


# LVS - Connections, cumulative
# This is just GUESSWORK! Maybe this will work
LVSCONN_CG_GTID=35

# LVS - Service traffic in
# This is just GUESSWORK! Maybe this will work
LVSTRAF_CG_GTID=36

# LVS - Connections, per port
# This is just GUESSWORK! Maybe this will work
# The following parameters will be used to get the list of managed ports,
# and to add a graph accordingly. These graphs will be of the ds type.
LVSCONN_GTID=35
LVSCONN_QID=10
LVSCONN_QTID=23
LVSCONN_FIELD=srvPort

# LVS - Service traffic in
# This is just GUESSWORK! Maybe this will work
LVSTRAF_GTID=36
LVSTRAF_QID=10
LVSTRAF_QTID=24
LVSTRAF_FIELD=srvPort


# You shouldn't need to edit anything below this line
########################################################################

ADD_GRAPHS=$(PHP) $(CLI_DIR)/add_graphs.php
ADD_TREE=$(PHP) $(CLI_DIR)/add_tree.php
ADD_DEVICE=$(PHP) $(CLI_DIR)/add_device.php
ADD_DATA_QUERY=$(PHP) $(CLI_DIR)/add_data_query.php
LIST_HOSTS=$(ADD_GRAPHS) --list-hosts

MATCH=$(or $(FQDN),$(NAME))
RESOLVE=$(if $(MATCH),$(shell $(LIST_HOSTS) | grep "$(MATCH)" | head -n 1 | awk '{ print $$1 }'))

# The following will set ID to the value of RESOLVE, but only if ID was not
# already defined
ID ?= $(RESOLVE)

.SILENT: nothing

nothing:
        echo "Impossible is nothing [$(RESOLVE)]"
        echo "FQDN: $(FQDN)"
        echo "NAME: $(NAME)"
        echo "ID:   $(ID)"

# add_host_to_cacti will require FQDN and NAME to be defined, so if
# this target is running, we are sure they are OK
# These variables are then passed along to the sub-instances of make
# automatically, hence there is no need to pass them along explicitly.
# Having them set, and not having ID manually set, will assign ID the
# id value of the newly added host. So, once again, no need to set it
# explicitly on the command line.
# I like this stuff :-D
all: add_host_to_cacti
        make add_all_graphs
        make add_host_to_tree


add_lvs_host: 
        make add_host_to_cacti HOST_TID=$(LVS_HOST_TID)
        make lvs
        make add_host_to_tree


interfaces: _idcheck
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=ds 
                --graph-template-id=$(IFACE_GTID) 
                --snmp-query-id=$(IFACE_QID) 
                --snmp-query-type-id=$(IFACE_QTID) 
                --snmp-field=$(IFACE_FIELD) 
                --snmp-value=$(IFACE_VALUE)

cpus: cpu_cumulative cpu_single

cpu_cumulative: _idcheck
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=cg 
                --graph-template-id=$(CPU_GTID)

cpu_single: _idcheck
        $(ADD_GRAPHS) 
                --list-snmp-values 
                --host-id=$(ID) 
                --snmp-query-id=$(CPUS_QID) 
                --snmp-field="$(CPUS_FIELD)" | 
        grep "^[0-9]" | 
        while read CPUID ; 
        do $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=ds 
                --graph-template-id=$(CPUS_GTID) 
                --snmp-query-id=$(CPUS_QID) 
                --snmp-query-type-id=$(CPUS_QTID) 
                --snmp-field="$(CPUS_FIELD)" 
                --snmp-value=$$CPUID ; 
        done

loadaverage: _idcheck
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=cg 
                --graph-template-id=$(LA_GTID)

memory: _idcheck
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=cg 
                --graph-template-id=$(MEM_GTID)

processes: _idcheck
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=cg 
                --graph-template-id=$(PROC_GTID)

usedspace: _idcheck
        $(ADD_GRAPHS) 
                --list-snmp-values 
                --host-id=$(ID) 
                --snmp-query-id=$(US_QID) 
                --snmp-field="$(US_FIELD)" | 
        grep -v "^Known" | 
        while read DEVID ; 
        do 
        if [ -z "$$DEVID" ] ; then continue ; fi ; 
        $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=ds 
                --graph-template-id=$(US_GTID) 
                --snmp-query-id=$(US_QID) 
                --snmp-query-type-id=$(US_QTID) 
                --snmp-field="$(US_FIELD)" 
                --snmp-value="$$DEVID" ; 
        done


lvs:  lvs_conn lvs_traffic

lvs_conn: _idcheck
        $(ADD_GRAPHS) 
                --list-snmp-values 
                --host-id=$(ID) 
                --snmp-query-id=$(LVSCONN_QID) 
                --snmp-field="$(LVSCONN_FIELD)" | 
        grep "^[0-9]" | 
        while read PORTID ; 
        do $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=ds 
                --graph-template-id=$(LVSCONN_GTID) 
                --snmp-query-id=$(LVSCONN_QID) 
                --snmp-query-type-id=$(LVSCONN_QTID) 
                --snmp-field="$(LVSCONN_FIELD)" 
                --snmp-value=$$PORTID ; 
        done

lvs_traffic: _idcheck
        $(ADD_GRAPHS) 
                --list-snmp-values 
                --host-id=$(ID) 
                --snmp-query-id=$(LVSTRAF_QID) 
                --snmp-field="$(LVSTRAF_FIELD)" | 
        grep "^[0-9]" | 
        while read PORTID ; 
        do $(ADD_GRAPHS) 
                --host-id=$(ID) 
                --graph-type=ds 
                --graph-template-id=$(LVSTRAF_GTID) 
                --snmp-query-id=$(LVSTRAF_QID) 
                --snmp-query-type-id=$(LVSTRAF_QTID) 
                --snmp-field="$(LVSTRAF_FIELD)" 
                --snmp-value=$$PORTID ; 
        done



add_host_to_cacti: _namecheck _fqdncheck
        $(ADD_DEVICE) 
                --description="$(NAME)" 
                --ip="$(FQDN)" 
                --template=$(HOST_TID)

add_host_to_tree: _idcheck
        $(ADD_TREE) 
                --type=node 
                --node-type=host 
                --tree-id=$(DEFTREE_ID) 
                --host-id=$(ID)

add_all_graphs: _idcheck interfaces cpus loadaverage memory processes usedspace

fulldump: _idcheck dump_hosts dump_host_templates dump_graph_templates dump_queries_and_types dump_snmp_fields_per_query_type dump_fields_and_values dump_input_fields


dump_hosts:
        @echo "# HOSTS"
        @$(LIST_HOSTS)

dump_host_templates:
        @echo "# HOST TEMPLATES"
        @$(ADD_DEVICE) --list-host-templates

dump_graph_templates:
        @echo "# GRAPH TEMPLATES"
        @$(ADD_GRAPHS) --list-graph-templates

dump_queries_and_types:
        @echo "# QUERIES AND TYPES"
        @$(ADD_GRAPHS) --list-snmp-queries | grep "^[0-9]" | 
        while read ID NAME ; do 
                echo "= $$ID - $$NAME =" ; 
                $(ADD_GRAPHS) --list-query-types  --snmp-query-id=$$ID ;
                 echo ; 
        done

dump_snmp_fields_per_query_type: _idcheck
        @echo "# SNMP FIELDS PER QUERY TYPE"
        @$(ADD_GRAPHS) --list-snmp-queries | grep "^[0-9]" | 
        while read ID NAME ; do 
                echo "= $$ID - $$NAME =" ; 
                $(ADD_GRAPHS) --list-snmp-fields 
                        --host-id=$(ID) 
                        --snmp-query-id=$$ID ; 
                echo ; 
        done

dump_fields_and_values: _idcheck
        @echo "# SNMP FIELDS AND VALUES"
        @$(ADD_GRAPHS) --list-snmp-queries | grep "^[0-9]" | 
        while read ID NAME ; do 
                echo "= $$ID - $$NAME =" ; 
                $(ADD_GRAPHS) --list-snmp-fields 
                        --host-id=$(ID) 
                        --snmp-query-id=$$ID | grep -v "^Known" | 
                while read FIELD ; do 
                        if [ -z "$$FIELD" ] ; then continue ; fi ; 
                        echo "== $$FIELD ==" ; 
                        $(ADD_GRAPHS) --list-snmp-values 
                                --host-id=$(ID) 
                                --snmp-query-id=$$ID  
                                --snmp-field="$$FIELD" ;
                        echo ; 
                done ; 
                echo ; 
        done

dump_input_fields:
        @echo "# INPUT FIELDS"
        @$(ADD_GRAPHS) --list-graph-templates | grep "^[0-9]" | 
        while read ID NAME ; do 
                echo "= $$ID - $$NAME =" ; 
                $(ADD_GRAPHS) --list-input-fields --graph-template-id=$$ID ; 
        done

_idcheck:
        @if [ -z "$(ID)" ] ; then echo "No ID specified" ; exit 1 ; fi
        @echo "Selected ID: $(ID)"


_namecheck:
        @if [ -z "$(NAME)" ] ; then 
                echo "Please specify a name/description for this device" ; 
                exit 2 ; 
        fi

_fqdncheck:
        @if [ -z "$(NAME)" ] ; then 
                echo "Please specify a fqdn or IP address for this device" ; 
                exit 3 ; 
        fi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s