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
LVL 1
petersystems engineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shalom CarmelCTOCommented:
Peter,
You are probably correct in your assessment. Why don't you try your revised code?
petersystems engineerAuthor Commented:
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
petersystems engineerAuthor Commented:
I solved this issue myself, although it required I specify a hostname for each seperate server in each file
Ideally I would like this script to be able to shut down any appserver



Soluton
self.zabbix_host_name = app_name + "-" + self.server_ip_address

idx = hostname.rfind('-')
   
    hostname = hostname[idx:]
    hostname = 'ip' + hostname
   
    host_prefix = hostname[0:idx]


parser.add_argument('-H', '--hostname', help='AWS host name', default="aws-appname-test")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

petersystems engineerAuthor Commented:
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
petersystems engineerAuthor Commented:
no one else replied
Shalom CarmelCTOCommented:
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?
petersystems engineerAuthor Commented:
I totally understand, I was pressed for time to get this solved quickly.
Best!

Peter
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.