Wenn die Hosts nur mit DNS-Namen angelegt sind wird regelmäßig der DNS-Cache aktualisiert, dies verzögert den Deploy Prozess. Das Script ließt den DNS Cache aus und setzt für die Hosts die IP Adresse statisch, ist die IP nicht im Cache löst er diese direkt per DNS auf.

#!/usr/bin/env python

#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY 
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with 
# this program; if not, write to the Free Software Foundation, Inc., 
# 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#

import json
import os
import subprocess
import sys
import livestatus
import socket

url = "http://cmkdev/dev1/check_mk/webapi.py"
user = "checkmk_automation"
passw = "password"
department = "hosttag"

##########################################################################################
### DON'T CHANGE ANYTHING BELOW
##########################################################################################

curl_base_cmd = url + "?_username=%s&_secret=%s" %(user,passw)

def execute_curl_cmd(curl_cmd):
    try:
        p = subprocess.Popen(curl_cmd, shell=True, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        o, e = p.communicate()
        out  = json.loads(o)
    except Exception, e:
        sys.stderr.write("ERROR: '%s' while executing '%s'\n" % (e, curl_cmd))
        sys.exit(1)
        return {}

    if out["result_code"] == 0:
        return out["result"]
    else:
        sys.stderr.write("ERROR: while executing WEB API command: '%s'\ncurl: '%s'\n" % \
                         (out["result"], curl_cmd))
        sys.exit(1)
        return {}

def edit_wato_host(curl_base_cmd, req):
    create_cmd = ('curl --insecure \'' + curl_base_cmd + \
                  '&action=edit_host\' ' + \
                  '-d \'%s\'') % req
    return execute_curl_cmd(create_cmd)

try:
    omd_root = os.getenv("OMD_ROOT")
    socket_path = "unix:" + omd_root + "/tmp/run/live"
    ip_cache = omd_root + "/var/check_mk/ipaddresses.cache"

    with open(ip_cache,'r') as inf:
        ipc = eval(inf.read())

except:
    sys.stderr.write("This example is indented to run in an OMD site\n")
    sys.stderr.write("Please change socket_path in this example, if you are\n")
    sys.stderr.write("not using OMD.\n")
    sys.exit(1)

hosts_arr = livestatus.SingleSiteConnection(socket_path).query_table("GET hosts\nColumns: name\nFilter: custom_variables ~~ TAGS %s" % department)

for host in hosts_arr:

    cached_ip = ipc.get((host[0], 4))

    if cached_ip != None:

        print host[0]
        print cached_ip

        req = 'request={"hostname": "%s", "attributes":{"ipaddress": "%s"}}' %(host[0],cached_ip)

        print req

        edit_wato_host(curl_base_cmd,req)

    else:

        try:
            ip = socket.gethostbyname(host[0])
            print "%s - %s" % (host[0],ip)

        except:
            print "no dns record for host %s " % host[0]

        req = 'request={"hostname": "%s", "attributes":{"ipaddress": "%s"}}' %(host[0],ip)

        print req

        edit_wato_host(curl_base_cmd,req)

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*