No Account Yet?

You are not logged in.

Add to: JBookmarks Add to: Facebook Add to: Windows Live Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Blogmarks Add to: Technorati Add to: Newsvine Add to: Google Information
Script to manage a custom built CMDB in Python using Threading E-mail
Programming HowTo's - Python HowTo's
Written by Allen Sanabria   
Tuesday, 17 June 2008 14:30

This script was created so that you can query/insert/update a MySQL database based on Project/Site/Hostname/Cluster/Rack and Environment. You can also  run multi threaded commands to the output of Project/Site/Hostname/Cluster/Rack and or Environment .

For instance to run 1 command to 200 boxes took about 5 seconds, man you got to love that. 

To update the CMDB... Usage: [ -u -c 'cluster' -d 'device' -s 'site' -p 'project' -e 'environment']
example.. cmdb-admin -u -d {hostname} -c {cluster} -s {site} -p {project} -r {rack} -e {environment}

To insert a new device in the CMDB... Usage: [-i -d 'device' -I 'ipaddr' -c 'cluster' -s 'site' -p 'project' -r 'rack' -e 'environment']
example.. cmdb-admin -i -d {hostname} -I {ipaddress} -c {cluster} -s {site} -p {project} -r {rack} -e {environment}


To run a command to the different clusters/sites/projects/racks Usage: [-c|-p|-s|-r|-d|-e -C '{command} [options of command] %s '
example.. cmdb-admin -c {cluster} -C "ping -c2 %s"
cmdb-admin -s {site} -C "ssh %s 'echo hello'"

To query the CMDB.... Usage: [-d 'device' -q] (-v is optional)
example.. cmdb-admin -q -d {hostname} -v

To query the CMDB.... Usage: [-c {cluster} -q] (-v is optional)
example.. cmdb-admin -q -c {cluster} -v

To query the CMDB.... Usage: [-p 'project' -q] (-v is optional)
example.. cmdb-admin -q -p WEB -v

To query the CMDB.... Usage: [-s 'device' -q] (-v is optional)
example.. cmdb-admin -q -s {site} -v

To query the CMDB.... Usage: [-r 'device' -q] (-v is optional)
example.. cmdb-admin -q -r {rack} -v

Enjoy the script below!

 

#!/bin/env python

#Copyright (C) 2008 Allen Sanabria

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

##################################################################
#Created by Allen Sanabria aka LinuxDynasty aka PrNino69
#This program will update/query/and even execute commands based
#on which cluster/project/site/ or rack you want to run it against
#completed, Thursday 21st 2008
##################################################################

import sys, os, MySQLdb, getopt, re, string, threading
from time import sleep, ctime

mydb = 'CMDB'
db_user = 'master'
db_passwd = 'paswd'
host_db = 'mysql_master'

try:
Con = MySQLdb.connect(host=host_db, port=3306, user=db_user, passwd=db_passwd, db=mydb)
except Exception, e:
print e
else:
Cursor = Con.cursor( )


def main(args):
if insert:
mysql_insert()
elif device and cluster and project and site and update and rack and env:
mysql_update()
elif device and query or cluster and query or site and query or project and query or rack
and query or env and query or device and command or cluster and command or site
and command or project and command or rack and command or env and command:
mysql_query()
elif help:
usage(0)
else:
usage(0)

def mysql_insert():
i_sql = "INSERT into host_info (host_id,host_name,ip_address,rack_info,site_name,cluster_name,project,environ)
VALUES (NULL,'%s','%s','%s','%s','%s','%s','%s')" % (device, ipaddr, rack, site, cluster, project,env)
Cursor.execute(i_sql)
Con.close( )

def mysql_update():
u_sql = "UPDATE host_info SET cluster_name = '%s', site_name = '%s', project = '%s', rack_info = '%s', environ = '%s'
where (host_name LIKE \"%s\")" % (cluster, site, project, rack, env, device1)
Cursor.execute(u_sql)
Con.close( )

class cmdb_command(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__( self )
self.name = name
self.func = func
self.args = args

def run(self):
apply(self.func, self.args)

def cmdb_exec(host, nu_cmd):
# print host + " started ", ctime()
os.system(nu_cmd)
# print host + " finished ", ctime() +"\n"


def mysql_query():
column = None
s_query = None
if project:
column = "project"
s_query = project
elif cluster:
column = "cluster_name"
s_query = cluster
elif site:
column = "site_name"
s_query = site
elif rack:
column = "rack_info"
s_query = rack
elif device:
column = "host_name"
s_query = device1
elif env:
column = "environ"
s_query = env


perc= "%"
sql = "SELECT host_name, ip_address, rack_info, cluster_name, site_name, project, environ FROM host_info
where (%s LIKE \"%s\")" % (column, s_query)
Cursor.execute(sql)
Results = Cursor.fetchall( )
Con.close( )
if len(Results) == 0:
print "Device " + device + " does not exist!"
exit
if command:
threads = []
nloops = range(len(Results))
started_time = ctime()
print "STARTING HERE", ctime()
for host in Results:
nu_cmd = command % (host[0])
after = cmdb_command(cmdb_exec, (host[0], nu_cmd), cmdb_exec.__name__)
threads.append(after)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
ended_time = ctime()
print "ENDING HERE", ctime()
print "The time this program started " + started_time + " and the time this program ended " + ended_time
else:
query_print(Results)

def query_print(Results):
i = 0
for line in Results:
try:
if verbose:
print "Hostname = " + Results[i][0] + ", Ipaddress = " +Results[i][1] + ",
Rack = " +Results[i][2] + ", Cluster = " + Results[i][3] + ",
Site = " + Results[i][4] + ", Project = " + Results[i][5] + ",
Environment = " +Results[i][6]
else:
print Results[i][0]
except IndexError, e:
nondevice = "Device " + device + " Does not exist"
raise nondevice
i += 1

def usage(code=0):

print """
To update the CMDB...
Usage: [ -u -c 'cluster' -d 'device' -s 'site' -p 'project' -e 'environment']
example..
cmdb-admin -u -d {hostname} -c {cluster} -s {site} -p {project} -r {rack} -e {environment}

To insert a new device in the CMDB...
Usage: [-i -d 'device' -I 'ipaddr' -c 'cluster' -s 'site' -p 'project' -r 'rack' -e 'environment']
example..
cmdb-admin -i -d {hostname} -I {ipaddress} -c {cluster} -s {site} -p {project} -r {rack} -e {environment}

To run a command to the different clusters/sites/projects/racks
Usage: [-c|-p|-s|-r|-d|-e -C '{command} [options of command] %s '
example..
cmdb-admin -c {cluster} -C "ping -c2 %s"
cmdb-admin -s {site} -C "ssh %s 'echo hello'"

To query the CMDB....
Usage: [-d 'device' -q] (-v is optional)
example.. cmdb-admin -q -d {hostname} -v

To query the CMDB....
Usage: [-c {cluster} -q] (-v is optional)
example..
cmdb-admin -q -c {cluster} -v

To query the CMDB....
Usage: [-p 'project' -q] (-v is optional)
example..
cmdb-admin -q -p WEB -v

To query the CMDB....
Usage: [-s 'device' -q] (-v is optional)
example..
cmdb-admin -q -s {site} -v

To query the CMDB....
Usage: [-r 'device' -q] (-v is optional)
example..
cmdb-admin -q -r {rack} -v

-c, --cluster= what cluster is it part of... example
-C, --command= what command to run
-d, --device= name of device
-s, --site= what site is it part of..
-p, --project= What project is it part of
-r, --rack= What rack the device is located in
-e, --env= environment
-u, --update update mode
-i, --insert insert mode
-I, --ipaddr IP Address of device
-q, --query query mode
-h, --help This help message
-v, --verbose Added verbosity

"""

try:
opts, args = getopt.getopt(sys.argv[1:], "d:c:p:s:r:e:C:I:hvuqi",
[ 'device=', 'cluster=', 'command=', 'site=', 'project=', 'rack=',
'ipaddr=', 'env=', 'help', 'update', 'query', 'insert', 'verbose' ]
)
except getopt.error:
usage(0)

help = verbose = cluster = site = project = rack = device = env = query = update = command = insert = ipaddr = None
for opt, val in opts:
if opt in ('-v', '--verbose'):
verbose = True
if opt in ('-q', '--query'):
query = True
if opt in ('-u', '--update'):
update = True
if opt in ('-i', '--insert'):
insert = True
if opt in ('-d', '--device'):
device = val
device1 = device + ".%"
if opt in ('-p', '--project'):
project = val
if opt in ('-c', '--cluster'):
cluster = val
if opt in ('-s', '--site'):
site = val
if opt in ('-r', '--rack'):
rack = val
if opt in ('-e', '--env'):
env = val
if opt in ('-C', '--command'):
command = val
if opt in ('-I', '--ipaddr'):
ipaddr = val

if __name__ == "__main__":
main(sys.argv[1:])

 

Comments
Search RSS
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 19 July 2008 14:01 )
 

 

Python News