说明
下面是我自己写一个小脚本。直接生成了Hosts Items Triggers,想直接生成之前,需要了解Items Triggers如何使用,和修改模板。
脚本目的
脚本目的:要导入大批量的交换机和WIFI,并且PING他们通不通,得到在ZABBIX上的返回信息。我需要来回创建Hosts Items
Triggers这三个值,耽误时间太多,就用PHP写了个脚本,其中Items
Triggers这两个的值都是我在模板文件内提前定义好的。我我只需要添加hosts名字和hosts
ip地址,点击SUBMIT,即可导入到ZABBIX内。
目录结构
\zabbix\xml\ 模板存放位置
\zabbix\zabbix.php 脚本
\zabbix\ZabbixApi.class.php API
\zabbix\ZabbixApiAbstract.class.php API
PHP代码
groups->group->name='New Wireless Lan';
$xml->hosts->host->host = $name;
$xml->hosts->host->name = $name;
$xml->hosts->host->groups->group->name='New Wireless Lan';
$xml->hosts->host->interfaces->interface->ip = $ip;
$xml->hosts->host->items->item->name = $name.'_AP_alive';
$xml->hosts->host->items->item->key = 'icmpping['.$ip.',10,100,64,1000]';
$xml->hosts->host->items->item->delay = '300';
$xml->triggers->trigger->expression = '{'.$name.':icmpping['.$ip.',10,100,64,1000].last(0)}=0';
$xml->triggers->trigger->name = $name.'_AP_alive';
// $file = fopen("zbx_export_hosts.xml");
$xml->saveXML("xml/zbx_export_hosts.xml");
###################Zabbix host 文件导入################################
$api = new ZabbixApi('', 'monitor', '1qaz2wsx');
$xml ='xml/zbx_export_hosts.xml';
$f = fopen ($xml, "r");
$source = stream_get_contents($f);
fclose ($f);
$params = array('format'=>'xml','rules'=>array('groups'=>array('createMissing'=>true),'hosts'=>array('createMissing'=>true,'updateExisting'=>true),'items'=>array('createMissing'=>true,'updateExisting'=>true),'triggers'=>array('createMissing'=>true,'updateExisting'=>true)),'source'=>$source);
print_r($params);
$result_4 = $api->configurationImport($params);
// 判断导入是否成功
if(!$result_4){
echo "error";
}
else{
echo "
";
echo $name." OK";
}
}
?>
阅读(4435) | 评论(0) | 转发(0) |