Demande #1017
ferméAnomalie #978: remonté des alertes lorsqu'un service ou une machine est planté
ajout d'une alerte zabbix via les API
Description
Expérimentations avec l'API zabbix
Utilisation¶
Ajout d'un host.
Obtention du token d'auth¶
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"user.authenticate", "params":{"user":"admin", "password":"XXX"}, "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":"a74d1f3f4c4e83ac2a6bbee704a49bc6","id":2}
Obtention de l'ID du template¶
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"template.get", "params":{ "output":"extend", "filter":{ "host":"Template_Linux" } }, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":{"10001":{"hostid":"10001","proxy_hostid":"0","host":"Template_Linux","dns":"","useip":"0","ip":"","port":"10050","status":"3","disable_until":"0","error":"","available":"0","errors_from":"0","lastaccess":"0","inbytes":"0","outbytes":"0","useipmi":"0","ipmi_port":"623","ipmi_authtype":"0","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_ip":"","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":"","templateid":"10001"}},"id":2}
Obtention de l'ID du groupe¶
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"template.get", "params":{ "output":"extend", "filter":{ "host":"Linux servers" } }, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":[],"id":2}
Creation du host¶
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"host.create", "params":{"host":"TEST1","ip":"192.168.44.44","port":10050,"useip":1,"groups":[{"groupid":2}],"templates":[{"templateid": 10001}]}, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":{"hostids":["10075"]},"id":2}
Import du host¶
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"configuration.import", "auth":"9f50b272bd93ef2ca775a9565f7888d9", "id":2, "params":{"format": "xml", "rules": {"hosts": {"createMissing": true, "updateExisting": true}, "templates": {"createMissing": true, "updateExisting": true}, "templateLinkage": {"createMissing": true} }, "source": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <zabbix_export> <version>2.0</version> <date>2012-11-17T21:23:31Z</date> <groups> <group> <name>Zabbix servers</name> </group> </groups> <hosts> <host> <host>HOST1</host> <name>HOST1</name> <proxy/> <status>0</status> <ipmi_authtype>-1</ipmi_authtype> <ipmi_privilege>2</ipmi_privilege> <ipmi_username/> <ipmi_password/> <templates> <template> <name>Template OS Linux</name> </template> </templates> <groups> <group> <name>Zabbix servers</name> </group> </groups> <interfaces> <interface> <default>1</default> <type>1</type> <useip>1</useip> <ip>127.0.0.1</ip> <dns/> <port>10050</port> <interface_ref>if1</interface_ref> </interface> </interfaces> <applications/> <items> </items> <discovery_rules/> <macros/> <inventory/> </host> </hosts> </zabbix_export>"}}' http://127.0.0.1/zabbix/api_jsonrpc.php
Installation¶
apt-get install rubygems gem install zabcon
Credentials¶
Dans l'interface web s'assurer que l'utilisateur a accès à l'API. Il y a une ligne Api Access et quand on click sur le lien Users sur la même ligne, on doit voir l'utilisateur qui va servir pour l'API.
Configuration¶
cp /var/lib/gems/1.8/gems/zabcon-0.0.392/zabcon.conf.default ~/zabcon.conf
Edition pour ajouter (tout est en commentaire par défaut):
server[april][server]=http://192.168.1.33/zabbix/ server[april][username]=admin server[april][password]=XXXXX default_server=april
Attention: essayer de faire la même chose via la ligne de commande avec login comme par exemple:
ephedrine:/# ruby1.8 /var/lib/gems/1.8/bin/zabcon.rb --no-login - Unable to find a default configuration file i486-linux Welcome to Zabcon. Build Number: 392 Use the command 'help' to get help on commands -> login http://192.168.1.33/zabbix/ admin XXXX WARNING: Initialization has changed, backwards compatability is being used. WARNING: Use ZabbixAPI.new(url,:debug=>n,:returntype=>:result) to have the WARNING: same capability as previous versions. WARNING: This depreciated functionality will be removed in a future release Runtime error detected (NoMethodError): undefined method `[]=' for nil:NilClass
va échouer parceque /var/lib/gems/1.8/gems/zabcon-0.0.392/./libs/zabbix_server.rb fait référence a un nom qui n'existe pas:
ServerCredentials.instance[@credentials["name"]]["auth"]= @connection.auth
et qui correspond au nom april dans le fichier de conf. C'est un signe que zabcon n'est utilisé qu'avec un fichier de conf.
Debug¶
Zabcon tracker fermé¶
Probablement pour des questions d'anti-spam le tracker est fermé (on ne peut pas s'enregistré) donc il n'est pas possible de remonter un bug. Il faudrait contacter les auteurs mais on dirait que c'est pas super actif.
Curl¶
Exemple de ligne de commande pour interagir avec l'API en direct:
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"user.authenticate", "params":{"user":"admin", "password":"XXXXX"}, "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":"08a3b7bd27b5d7bbfcc1e75111d75c16","id":2}
Logs¶
Il n'y a pas de logs de l'API sur le serveur zabbix par défaut (peut-etre augmenter la verbosité ?). On voit passer les requêtes dans les logs apache de l'interface web tail -f /var/log/apache2/.log* qui se trouve sur le vserver dokuwiki de ns1.april.org dans /var/www/april.lan/www/zabbix
92.168.1.50 - - [11/Nov/2012:14:34:17 +0100] "POST /zabbix//api_jsonrpc.php HTTP/1.1" 200 39 "-" "Zbx Ruby CLI"
Mis à jour par Loïc Dachary il y a environ 12 ans
irc.freenode.net#zabbix
(12:54:00 PM) dachary: Hi, I would like to use puppet to add items/triggers to zabbix. Any suggestions as to where I could start reading about this ? (12:54:58 PM) f0: dachary: http://www.zabbix.com/documentation/2.0/api
Mis à jour par Loïc Dachary il y a environ 12 ans
- % réalisé changé de 0 à 10
Je préferre ruby parceque puppet est en ruby. Donc je tente d'abord l'option API implémenté en ruby et ça tombe bien parceque c'est la seule API qui semble avoir une command line.
Installation de zabcon.
http://trac.red-tux.net/
ephedrine:/# apt-get install rubygems ... ephedrine:/# gem install zabcon
Mis à jour par Loïc Dachary il y a environ 12 ans
Quid d'un upgrade vers zabbix-2.0.x ? irc.freenode.net#zabbix says:
(09:33:24 PM) dachary: volter: I guess my question is about how safe an upgrade from 1.8.x to 2.0.x is. It is 100% safe and moron proof ? Or does it require some kind of plan. (09:57:40 PM) Richlv: some plan. and a backup. (10:01:35 PM) Steki: and most of your templates will not work correctly (10:01:45 PM) Steki: or better say they will but new functionality will not (10:01:52 PM) Steki: so plan ahead (10:50:00 PM) volter: dachary: Some items were dropped in 2.0, so you might also see that. (10:50:23 PM) volter: If you use DM, be aware you must upgrade other nodes or proxies as well.
Mis à jour par Loïc Dachary il y a environ 12 ans
- http://www.zabbix.com/documentation/1.8/api
- http://www.zabbix.com/documentation/2.0/manual/appendix/api/api
- support de zabbix API 2.0 avec ruby https://groups.google.com/forum/?fromgroups=#!topic/zabcon/Idq2awH78SI
Lecture de la documentation de zabcon
Mis à jour par Loïc Dachary il y a environ 12 ans
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"user.authenticate", "params":{"user":"admin", "password":"XXXX"}, "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"No API access"},"id":2}
C'est parceque l'utilisateur n'a pas les droits d'accès à l'API, bien qu'il soit admin comme on le voit dans "l'interface de gestion des utilisateurs":
http://127.0.0.1:8080/zabbix/usergrps.php
Mis à jour par Vincent-Xavier JUMEL il y a environ 12 ans
Mis à jour par Loïc Dachary il y a environ 12 ans
Pour avoir la liste des commandes zabcon help commands. Mais ça ne dit pas ce que ça fait ni quels sont les arguments. Pour avoir la liste des hosts c'est get host alors que la fonction d'API correspondante est host.get . Je laisse tomber zabcon parcequ'il me faudrait lire le code pour arriver à l'utiliser de la façon la plus simple.
En utilisant curl (voir plus haut pour l'obtention du token auth:
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"host.get", "params":{"filter":{"host":["host_ns1"]},"output":"extend"}, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix/api_jsonrpc.php {"jsonrpc":"2.0","result":[{"hostid":"10047","proxy_hostid":"0","host":"host_ns1","dns":"","useip":"1","ip":"192.168.1.254","port":"10050","status":"0","disable_until":"0","error":"","available":"1","errors_from":"0","lastaccess":"0","inbytes":"0","outbytes":"0","useipmi":"0","ipmi_port":"623","ipmi_authtype":"-1","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_ip":"","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":""}],"id":2}
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"host.exists", "params":{"hostid":"10047"}, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":true,"id":2}
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"template.get", "params":{ "output":"extend", "filter":{ "host":"Template_Linux" } }, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":{"10001":{"hostid":"10001","proxy_hostid":"0","host":"Template_Linux","dns":"","useip":"0","ip":"","port":"10050","status":"3","disable_until":"0","error":"","available":"0","errors_from":"0","lastaccess":"0","inbytes":"0","outbytes":"0","useipmi":"0","ipmi_port":"623","ipmi_authtype":"0","ipmi_privilege":"2","ipmi_username":"","ipmi_password":"","ipmi_disable_until":"0","ipmi_available":"0","snmp_disable_until":"0","snmp_available":"0","maintenanceid":"0","maintenance_status":"0","maintenance_type":"0","maintenance_from":"0","ipmi_ip":"","ipmi_errors_from":"0","snmp_errors_from":"0","ipmi_error":"","snmp_error":"","templateid":"10001"}},"id":2}
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"template.get", "params":{ "output":"extend", "filter":{ "host":"Linux servers" } }, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":[],"id":2}
curl -H content-type:application/json --data '{ "jsonrpc":"2.0", "method":"host.create", "params":{"host":"TEST1","ip":"192.168.44.44","port":10050,"useip":1,"groups":[{"groupid":2}],"templates":[{"templateid": 10001}]}, "auth":"a74d1f3f4c4e83ac2a6bbee704a49bc6", "id":2 }' http://127.0.0.1:8080/zabbix//api_jsonrpc.php {"jsonrpc":"2.0","result":{"hostids":["10075"]},"id":2}
Mis à jour par Loïc Dachary il y a environ 12 ans
- Statut changé de Nouveau à Fermé
- % réalisé changé de 50 à 100
Mis à jour par Loïc Dachary il y a presque 12 ans
- Priorité changé de Normale à Immédiate