添加一个监控项目(不包括添加模板)涉及到几个表,分别是:ids, hosts, applications, groups, hosts_groups, interface, items,items_applications,如果还需要添加触发器的话还涉及到

functionstriggers等表。

 

基本的流程是这样的:

select table_name,nextid 

  from ids 

      where table_name="applications" 

          or table_name="hosts" 

          or table_name="groups" 

          or table_name="hosts_groups" 

          or table_name="interface" 

          or table_name="items" 

          or table_name="items_applications"

 

    这里搜索的是ids表,这个表存放了其他表的maxid值(因为zabbix里面,id值一般都是作为表的主键,不能出现重复的,插入的时候要先从这个表里面查到上一条语句的id是多少,新插入的id要在这个maxid的基础上加一,即下面用到id的地方,比如hostid的值是上面查到的hostid + 1

 

#添加主机

insert into hosts (hostid,host,name)  value ('10503','hk-192.168.1.1','xx云主机');

 这一步是添加一台主机到hosts表,hostid就用刚刚查到的值,host即该主机的主机名,name是该主机的别名

 

#添加对应应用集到主机

insert into applications(applicationid,hostid,name) value ('775','10504','网络检测')

 这一步是添加应用级到主机,name是应用集的名称,如果添加多个应用集就插入多条

 

#添加新的主机组

insert into groups groupid,name,internal value ('28','test1','0');

 如果想加入新的主机组,可以这样插入,当然如果把主机加入到已有的主机组,可以忽略这步

 

#添加主机到主机组

insert into hosts_groups(hostgroupid,hostid,groupid) value ('512','10504','28');

 这步就很简单了,把新加入的主机分组

 

#添加接口

insert into interface(interfaceid,hostid,main,type,ip,port) value ('426', '10510', '1', '1', '127.0.0.1','10050');

 添加主机的接口,type是接口的类型:1代表代理接口,2代表snmp接口,3JMX接口,4IPMI接口,main的话用zabbix原来的方法加了几次,发现同类型的第一个是1,然后再加的就为0.

 

#添加项目到对应主机

insert into items (itemid, type, hostid, name, key_, delay, history, trends, value_type, units,params,interfaceid,description) value ('27456','3','10504','丢包率','icmppingloss[192.168.1.1]', '10','30','365','3', '%','null','420','this is for test');

 这步就可以添加监控项目到主机了,tpye代表项目的检测类型,即主动,被动,简单等,这里的3是简单检测,name是项目名,key_是项目的键值,delay是检测间隔,history是历史保存时间,trends是趋势保存时间,value_type是数据的类型,units是单位,params是参数,interfaceid接口 id, description为项目描述。

 

#添加项目到应用集

insert into items_applications(itemappid,applicationid,itemid) value ("9869", "775", "27456") 

 最后还可以把项目添加到应用集,打开zabbix页面,可以看到监控已经添加上去了