您的位置:首页 > 其它

如何使用cacti脚本批量添加主机

2010-06-03 12:20 302 查看
使用脚本添加的时候遇到下面的错误。

[root@SJSWT44-122 cli]# php -q add_device.php
--description='user1.db' --ip='user1.db.d.xiaonei.com' --template=11

PHP Notice: Use of undefined constant E_DEPRECATED - assumed
'E_DEPRECATED' in /usr/local/apache/htdocs/cacti/include/global.php on
line 181

ERROR: Invalid snmp version (0)

本来想着暴力的去修改数据库,后来查看了add_device.php脚本之后发现脚本读取了一些配置文件,所以还是悠着点比较好。

于是在脚本里面搜索了一下Invalid snmp version,发现了使用add_device.php添加的时候不允许snmp_ver等于0,但是手工添加的实际上是允许0存在的,^_^于是只好修改一下代码了(注释掉)

/* process snmp information
if ($snmp_ver != "1" && $snmp_ver != "2" && $snmp_ver != "3") {
echo "ERROR: Invalid snmp version ($snmp_ver)/n";
exit(1);
}else{
if ($snmp_port <= 1 || $snmp_port > 65534) {
echo "ERROR: Invalid port.  Valid values are from 1-65534/n";
exit(1);
}
if ($snmp_timeout <= 0 || $snmp_timeout > 20000) {
echo "ERROR: Invalid timeout.  Valid values are from 1 to 20000/n";
exit(1);
}
}
*/
/* community/user/password verification
if ($snmp_ver == "1" || $snmp_ver == "2") {
/* snmp community can be blank
}else{
if ($snmp_username == "" || $snmp_password == "") {
echo "ERROR: When using snmpv3 you must supply an username and password/n";
exit(1);
}
}
*/


下面是我的批量添加脚本,大概思路:①从存放所有服务器信息的主机中获取主机信息②将每一个主机添加到cacti中③从cacti的数据库中获取刚添加的主机host-id④添加graphs(需要提前知道相应graph的id,因为是批量添加,这部分信息应该是固定的)⑤将主机添加到tree中

#!/usr/bin/python
import MySQLdb
import subprocess
conn=MySQLdb.connect(host='**',port=3306,user='**',passwd='**',db='**',charset='utf8')
cursor=conn.cursor()
cursor.execute("select id,ip,port from db limit 2,10")
hosts=cursor.fetchall()
cacti_conn=MySQLdb.connect(host="**",port=3306,user='**',passwd='**',db='cacti')
cacti_cursor=cacti_conn.cursor()
for host in hosts:
db_id=host[0]
ip=host[1]
port=host[2]
###add_device
sp_add_host=subprocess.Popen(["php","-q","/usr/local/apache/htdocs/cacti/cli/add_device.php","--description=%s:%s"%(ip,port),"--ip=%s"%ip,/
"--template=11"],shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print sp_add_host.stdout.read()
print sp_add_host.stdout.read()
###add_graph
cacti_cursor.execute("select id from host where description='%s:%s'"%(ip,port))
host_id_set=cacti_cursor.fetchone()
if host_id_set is None:
print "%s:%s host add failed"%(ip,port)
continue
host_id=host_id_set[0]
for graph_id in range(81,97):
if graph_id not in [84,85,89]:
sp_add_graph=subprocess.Popen(["php","-q","/usr/local/apache/htdocs/cacti/cli/add_graphs.php","--host-id=%s"%host_id,"--graph-type=cg",/
"--graph-template-id=%s"%graph_id,"--input-fields=db_id=%s"%db_id],shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print sp_add_graph.stdout.read()
print sp_add_graph.stdout.read()
##add_tree
sp_add_tree=subprocess.Popen(["php","-q","/usr/local/apache/htdocs/cacti/cli/add_tree.php","--type=node","--node-type=host","--tree-id=6","--host-id=%s"%host_id],/
shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print sp_add_tree.stdout.read()
print sp_add_tree.stdout.read()
cacti_cursor.close()
cacti_conn.close()
cursor.close()
conn.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: