miércoles, 18 de abril de 2018

Script - Zabbix monitoring, automatic templates and hosts deployments

Here we have an script to populate a Zabbix 3.2 deployment with templates and hosts, previously exported (xml) from another Zabbix 3.2 installation.

apiUrl="http://<zabbix_server_ip>:<zabbix_server_port>/zabbix/api_jsonrpc.php"

## Getting Auth Token:
jsonAuthData='{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"<Admin_password>"},"auth":null,"id":0}'
authToken=$(curl -i -X POST -H 'Content-Type:application/json' -d $jsonAuthData $apiUrl 2>/dev/null | grep result | cut -d'"' -f 8)

# Import Templates
jsonTemplates='{"jsonrpc": "2.0",
"method": "configuration.import",
"params": {
    "format": "xml",
    "rules": {
        "groups": {
            "createMissing": true},
        "applications": {
            "createMissing": true,
            "updateExisting": true},
        "templates": {
            "createMissing": true,
            "updateExisting": true},
    "items": {
        "createMissing": true,
        "updateExisting": true,
        "deleteMissing": true}},
    "source": "'$(cat zbx_export_templates.xml|sed s#\"#\\\\\"#g)'"},"auth": "'${authToken}'","id": 1}'
echo $jsonTemplates|curl -i -X POST $apiUrl -H 'Content-Type:application/json' -d @-

# Import HOSTS
jsonHosts='{
"jsonrpc": "2.0",
"method": "configuration.import",
"params": {
    "format": "xml",
    "rules": {
        "templateLinkage": {
            "createMissing": true
        },
        "groups": {
            "createMissing": true
        },
        "hosts": {
            "createMissing": true,
            "updateExisting": true
        },
        "items": {
            "createMissing": true,
            "updateExisting": true,
            "deleteMissing": true
        }
    },
    "source": "'$(cat zbx_export_hosts.xml|sed s#\"#\\\\\"#g)'"},"auth": "'${authToken}'","id": 1}'
echo $jsonHosts|curl -i -X POST $apiUrl -H 'Content-Type:application/json' -d @-


Where, zbx_export_templates.xml and zbx_export_hosts.xml are files generated previously by hand (big effort) or by exporting the appropiate templates and hosts from an already configured Zabbix 3.2 environment:



This way we can automate the Zabbix monitoring environment provisioning without any post configuration manual steps.

Hope this can help you, please feel free to contact Sepalo Software if you want more information about infrastructure/monitoring automation.