天天看點

Cacti-Autoinstall

#!/usr/bin/python  

#  

import os,shutil,urllib,MySQLdb,sys,subprocess  

cacti = ['cacti-0.8.7g.tar.gz','cacti-plugin-0.8.7g-PA-v2.8.tar.gz','rrdtool-1.4.4.tar.gz']  

change = ''.join(cacti[:1])  

rrd = ''.join(cacti[2])  

pl = ''.join(cacti[1])  

#If install files not exists,downloading now.  

for path in cacti:  

    if not os.path.exists(path):  

        web = 'http://xxx.xxx.com/shell/'+path   

    print '"%s" Downloading ......'%(path)  

        web_file = urllib.urlopen(web)  

        local = open(path,'wb').write(web_file.read())  

        web_file.close()  

www = '/var/www/cacti'#Cacti directory  

command = 'tar -zxf %s' 

#Decompress files  

    os.system(command % path)  

#Configure "Catci"  

if  os.path.exists(www):  

    shutil.rmtree(www)  

    print 'Delete %s directiory'%(www)  

if not os.path.exists(www):  

    shutil.move(change[0:12],www)  

    os.system('chmod -R 775 %s'%www)  

    print 'Create %s directiory'%(www)  

#Mysql datebase  

try:  

    conn = MySQLdb.connect(host='localhost',user='root',passwd='')  

except Exception,e:  

    print e  

    sys.exit()  

cursor = conn.cursor()   

if os.path.isdir('/var/lib/mysql/cacti'):  

    sql = "drop database cacti;create database cacti;grant all on cacti.* to cacti@localhost identified by '123456';flush privileges;" 

    cursor.execute(sql)  

else:  

    sql = "create database cacti;grant all on cacti.* to cacti@localhost identified by '123456';flush privileges;" 

cursor.close()  

conn.close()  

#Command  

com = "./configure --prefix='/usr/local/rrdtool' 1>/dev/null;make -j10 1>/dev/null;make install 1>/dev/null" 

#RRDTOOL  

if not os.path.isdir('/usr/local/rrdtool'):  

    os.chdir(rrd[:13])  

    os.system(com)  

#cacti-plugin  

#if not os.path.isfile(www + os.sep + 'pa.sql'):  

if not os.path.isdir(pl[:13]+'arch'):  

    os.chdir('../' + pl[:13] + 'arch')   

    shutil.copy('pa.sql',www)  

    shutil.copy('cacti-plugin-0.8.7g-PA-v2.8.diff',www)  

    os.chdir(www)  

    os.system("patch -p1 -N <cacti-plugin-0.8.7g-PA-v2.8.diff;mysql -uroot -p cacti<pa.sql")   

    os.system("mysql -uroot -p cacti<cacti.sql")   

    os.chdir(pl[:13] + 'arch')  

#net-snmp  

if not os.path.isfile('/usr/sbin/snmpd'):  

    os.system('yum install -y install net-snmp*')  

    os.system('chkconfig --level 35 snmpd on')  

    os.system('service snmpd start')  

if not os.path.isfile('/usr/local/bin/rrdtool'):  

    os.system('ln -sf /usr/local/rrdtool/bin/rrdtool /usr/local/bin/rrdtool')  

if not os.path.isfile('/usr/bin/php'):  

    os.system('ln -sf /usr/local/php/bin/php /usr/bin/php')  

#Crontab   

cron = '/etc/crontab' 

file = open(cron,'rw+')  

phpcron = '*/5 * * * * root php /var/www/cacti/poller.php > /dev/null 2>&1\n' 

for list in file:  

    if list == phpcron:  

        sys.exit()  

file.write(phpcron)  

file.close  

本文轉自 ibelieveme 51CTO部落格,原文連結:http://blog.51cto.com/skyson/438595,如需轉載請自行聯系原作者