Link to home
Start Free TrialLog in
Avatar of peter
peterFlag for United States of America

asked on

zabbix remove host using python script

Hi everyone,
I have the attached python script which removes a aws zabbix server from the zabbix console, on a aws auto scale event, meaning, when a server is terminated in aws, the script is executed upon server shutdown from /etc/rc.d
I verified the script does work, but only works if the aws instance name is in the default format of ip-10.0.0.0

All hostnames are in the format of:  aws-appname-prod-10.0.0.0

Which kicks off this error:
018-01-26 21:44:40,490 ip-10.1.2.254 urllib3.connectionpool INFO     Starting new HTTP connection (1): 192.168.105.18
2018-01-26 21:44:40,636 ip-10.1.2.254 root       INFO     Connected to Zabbix API Version 3.2.11
2018-01-26 21:44:40,636 ip-10.1.2.254 root       INFO     Retrieving host id for host "ip-10-1-2-254" from Zabbix server
2018-01-26 21:44:40,681 ip-10.1.2.254 root       CRITICAL There was a problem retrieving host id of host "ip-10-1-2-254 from Zabbix server "192.168.105.18"
Traceback (most recent call last):
  File "./remove_zabbix.py", line 106, in _get_zabbix_host_id
    zabbix_hostid = zhost[0]["hostid"]
IndexError: list index out of range


How can I modify this script to the my new hostname schema?

The appname can be one of 5 different names
The prod field can be test or prod
As will as the ip address will be set to the ip address of the machine after it boots.

ELSE

How can I modifying this script so it deletes the server from the main zabbix server console using the ip address of the aws instance instead of the hostname.

Seems this line is the one that should be modified:

self.zabbix_host_name = "ip-" + self.server_ip_address.replace(".", "-")


I believe this section need to be modified to retrieve the hostname in a different format:

def _get_zabbix_host_id(self, zabbix_host_name):
        """This function will retrieve the host id for the zabbix host"""
        logging.info('Retrieving host id for host "%s" from Zabbix server' % (zabbix_host_name))
        try:
            zhost = self.zabbix_conn.host.get(filter={"host": str(zabbix_host_name)})
            print(str(zhost))
            zabbix_hostid = zhost[0]["hostid"]
            logging.info('Host id "%s" was retrieved for hostname "%s"' % (zabbix_hostid, zabbix_host_name))
            return zabbix_hostid

Thanks,

Peter
remove_zabbix.py.origional.txt
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Peter,
You are probably correct in your assessment. Why don't you try your revised code?
Avatar of peter

ASKER

No, you dont understand, that bit of code is take from the attachment.  It only works if the hostname is in the format of ip-10.0.0.0

If I change the hostname to format of aws-appname-prod-10.0.0.0, I get a error:

"CRITICAL There was a problem retrieving host id of host "ip-10-1-2-254 from Zabbix server "192.168.105.18" "


So I changed:
self.zabbix_host_name = "ip-" + self.server_ip_address.replace(".", "-")
TO
self.zabbix_host_name = "aws-appname-test-" + self.server_ip_address

and it worked!

So now I need to figure how how to write a function that looks at 5 different appnames, if it find one, then it uses that one. Meaning, appname1, appname2 etc
ASKER CERTIFIED SOLUTION
Avatar of peter
peter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter

ASKER

Attaching full script which deletes any aws server based on its hostname. Simply add your credentials on the bottom and the hostname of the aws server.
remove_zabbix_prod_test_template.py.txt
Avatar of peter

ASKER

no one else replied
Perfect!!
Speaking on behalf of the EE volunteers, sometimes we have home/work/RL issues that block us from deep diving into complex solutions.

I am glad that you solved your problem. If you had to improve it - what would you want to do?
Avatar of peter

ASKER

I totally understand, I was pressed for time to get this solved quickly.
Best!

Peter