Monitoring IBM v7000 using Graphite and Grafana

From MyWiki

Jump to: navigation, search

Cron job

*/5 * * * * /home/alex/bin/v7k2graphite.py /home/alex/bin/v7k2graphite.ini

The script /home/alex/bin/v7k2graphite.py collects 5 minutes history and shoots it to Graphite:

#!/usr/bin/python2.7
# Filename: v7k2graphite.py

import statsd
import sys
import os
import string
import paramiko
import ConfigParser
import time
import socket


def read_config(section):  
	cfg = {}

	# Read ini-style configuration file
	try:
	   config = ConfigParser.ConfigParser()
	   config.readfp(open(cfg_file))
	except IOError as e:
	   print "I/O error({0}): {1}".format(e.errno, e.strerror)
	   sys.exit("Please check the configuration file " + cfg_file + "exists!")
	except:
	   print "Unexpected error:", sys.exc_info()[0]
	   raise
	   sys.exit(1)

	try:
	   # graphite server details
	   cfg['host'] = config.get(section,'host')
	   cfg['port'] = config.get(section,'port')

	   # SSH configuration to get to SAN storage
	   cfg['ssh_host'] = config.get(section,'ssh_host')
	   cfg['ssh_username'] = config.get(section,'ssh_username')
	   cfg['ssh_key'] = config.get(section,'ssh_key')

	   # what metrics to collect
	   cfg['metric'] = config.get(section, 'metric')
	
	except:
	  sys.exit("Error reading the configuration parameters from " + cfg_file + ". Exiting...")

	return cfg

def get_stats( ssh_host, ssh_username, ssh_key, cmd) :
	# Establish SSH connection with SAN storage management interface
	try:
	  ssh = paramiko.SSHClient()
	  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	  ssh.connect(ssh_host, username=ssh_username, password='', key_filename=ssh_key)
  
	except:
	  sys.exit("Error establishing SSH connection to server (" + ssh_username + "@" + ssh_host + ")!")

	stdin, stdout, stderr = ssh.exec_command(cmd)

	stdout = stdout.readlines()
	ssh.close()

	return stdout

def send_msg(host, port, message):
#    print 'sending message:\n%s to %s:%s' % (message, host, port)
    sock = socket.socket()
    sock.connect((host, int(port)))
    sock.sendall(message)
    sock.close()
    	
if __name__ == '__main__':
	help_str = "\nUsage: " + sys.argv[0] + " <configuration_file>\n"
	
	# parsing command line parameters first
	# if we don't have it the right way - exit
	if len(sys.argv) < 2:
		print help_str
		sys.exit(1)
	else:
		cfg_file = sys.argv[1]
		if not os.path.exists(cfg_file):
			print "Configuration file " + cfg_file + " doesn't exist!"
			sys.exit(1)
	
	config = {}
	config = read_config('graphite')
	
	# two controllers in the SAN
	nodes = ['node1','node2']
	
	for metric in config['metric'].split(',') :
		for node in nodes :
			out = get_stats(config['ssh_host'], config['ssh_username'], config['ssh_key'], 'lsnodecanisterstats -history ' + metric + ' -nohdr ' + node)
			for line in out :	
			# 1 node1 140911134627 mdisk_r_mb 0
				timestamp = int(time.mktime(time.strptime(line.split()[2], '%y%m%d%H%M%S')))
				send_msg(config['host'], config['port'], "storage"+"."+line.split()[1]+"."+line.split()[3]+" "+line.split()[4]+" "+str(timestamp)+"\n")

Ini file /home/alex/bin/v7k2graphite.ini

[graphite]
host = 10.10.10.10
port = 2003

ssh_host = 10.5.5.5
ssh_username = zabbix
ssh_key = /home/alex/.ssh/zabbix_v7000_rsa
metric = mdisk_w_mb,mdisk_r_mb,mdisk_w_io,mdisk_w_io,mdisk_w_ms,mdisk_r_ms,vdisk_w_mb,vdisk_r_mb,vdisk_w_io,vdisk_w_io,vdisk_w_ms,vdisk_r_ms,fc_mb,fc_io,sas_mb,sas_io,write_cache_pc,total_cache_pc,cpu_pc

The end result looks like this v7000-monitoring-with-Graphana.png

Personal tools