Cacti-Autoinstall

 

  
  
  
  
  1. #!/usr/bin/python  
  2. #  
  3. import os,shutil,urllib,MySQLdb,sys,subprocess  
  4.  
  5. cacti = ['cacti-0.8.7g.tar.gz','cacti-plugin-0.8.7g-PA-v2.8.tar.gz','rrdtool-1.4.4.tar.gz']  
  6. change = ''.join(cacti[:1])  
  7. rrd = ''.join(cacti[2])  
  8. pl = ''.join(cacti[1])  
  9. #If install files not exists,downloading now.  
  10. for path in cacti:  
  11.     if not os.path.exists(path):  
  12.         web = 'http://xxx.xxx.com/shell/'+path   
  13.     print '"%s" Downloading ......'%(path)  
  14.         web_file = urllib.urlopen(web)  
  15.         local = open(path,'wb').write(web_file.read())  
  16.         web_file.close()  
  17.  
  18. www = '/var/www/cacti'#Cacti directory  
  19. command = 'tar -zxf %s' 
  20.  
  21. #Decompress files  
  22. for path in cacti:  
  23.     os.system(command % path)  
  24.  
  25. #Configure "Catci"  
  26. if  os.path.exists(www):  
  27.     shutil.rmtree(www)  
  28.     print 'Delete %s directiory'%(www)  
  29. if not os.path.exists(www):  
  30.     shutil.move(change[0:12],www)  
  31.     os.system('chmod -R 775 %s'%www)  
  32.     print 'Create %s directiory'%(www)  
  33.  
  34. #Mysql datebase  
  35. try:  
  36.     conn = MySQLdb.connect(host='localhost',user='root',passwd='')  
  37. except Exception,e:  
  38.     print e  
  39.     sys.exit()  
  40.  
  41. cursor = conn.cursor()   
  42. if os.path.isdir('/var/lib/mysql/cacti'):  
  43.     sql = "drop database cacti;create database cacti;grant all on cacti.* to cacti@localhost identified by '123456';flush privileges;" 
  44.     cursor.execute(sql)  
  45. else:  
  46.     sql = "create database cacti;grant all on cacti.* to cacti@localhost identified by '123456';flush privileges;" 
  47.     cursor.execute(sql)  
  48. cursor.close()  
  49. conn.close()  
  50.  
  51. #Command  
  52. com = "./configure --prefix='/usr/local/rrdtool' 1>/dev/null;make -j10 1>/dev/null;make install 1>/dev/null" 
  53. #RRDTOOL  
  54. if not os.path.isdir('/usr/local/rrdtool'):  
  55.     os.chdir(rrd[:13])  
  56.     os.system(com)  
  57. #cacti-plugin  
  58. #if not os.path.isfile(www + os.sep + 'pa.sql'):  
  59. if not os.path.isdir(pl[:13]+'arch'):  
  60.     os.chdir('../' + pl[:13] + 'arch')   
  61.     shutil.copy('pa.sql',www)  
  62.     shutil.copy('cacti-plugin-0.8.7g-PA-v2.8.diff',www)  
  63.     os.chdir(www)  
  64.     os.system("patch -p1 -N <cacti-plugin-0.8.7g-PA-v2.8.diff;mysql -uroot -p cacti<pa.sql")   
  65.     os.system("mysql -uroot -p cacti<cacti.sql")   
  66. else:  
  67.     os.chdir(pl[:13] + 'arch')  
  68.     shutil.copy('pa.sql',www)  
  69.     shutil.copy('cacti-plugin-0.8.7g-PA-v2.8.diff',www)  
  70.     os.chdir(www)  
  71.     os.system("patch -p1 -N <cacti-plugin-0.8.7g-PA-v2.8.diff;mysql -uroot -p cacti<pa.sql")   
  72.     os.system("mysql -uroot -p cacti<cacti.sql")   
  73. #net-snmp  
  74. if not os.path.isfile('/usr/sbin/snmpd'):  
  75.     os.system('yum install -y install net-snmp*')  
  76.     os.system('chkconfig --level 35 snmpd on')  
  77.     os.system('service snmpd start')  
  78. if not os.path.isfile('/usr/local/bin/rrdtool'):  
  79.     os.system('ln -sf /usr/local/rrdtool/bin/rrdtool /usr/local/bin/rrdtool')  
  80. if not os.path.isfile('/usr/bin/php'):  
  81.     os.system('ln -sf /usr/local/php/bin/php /usr/bin/php')  
  82. #Crontab   
  83. cron = '/etc/crontab' 
  84. file = open(cron,'rw+')  
  85. phpcron = '*/5 * * * * root php /var/www/cacti/poller.php > /dev/null 2>&1\n' 
  86. for list in file:  
  87.     if list == phpcron:  
  88.         sys.exit()  
  89. file.write(phpcron)  
  90. file.close  
  91.  

 

你可能感兴趣的:(职场,cacti,休闲)