Skip to content

Commit

Permalink
Various fixes to the Zabbix system on esdc-mon (#119)
Browse files Browse the repository at this point in the history
* Various fixes to the Zabbix system on esdc-mon
related to erigones/esdc-factory#29, erigones/esdc-factory#30 and other commits in the 2.5.2 release of esdc-mon appliance

* Remove old rabbitmq check after t_svc-mq upgrade
related to commit erigones/esdc-factory#7d25bf38c942fecde153e9e3b6a2aa72d951442d

* More monitoring fixes - commit erigones/esdc-factory#3ad5f55

* Updated Zabbix library modules (PyCharm inspection)
  • Loading branch information
dn0 authored Mar 25, 2017
1 parent 8a8bdbb commit 782c6fe
Show file tree
Hide file tree
Showing 27 changed files with 9,885 additions and 40 deletions.
69 changes: 69 additions & 0 deletions ans/library/zabbix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2012-2017, Erigones, s. r. o
try:
from zabbix_api import ZabbixAPI, ZabbixAPIException
except ImportError:
ZabbixAPI = ZabbixAPIException = None

from ansible.module_utils.basic import AnsibleModule


def main():
# noinspection PyShadowingBuiltins
module = AnsibleModule(
argument_spec=dict(
server_url=dict(required=True, default=None, aliases=['url']),
login_user=dict(required=True),
login_password=dict(required=True),
api_method=dict(required=True),
api_params_dict=dict(required=False, type='dict', default={}),
api_params_list=dict(required=False, type='list', default=[]),
timeout=dict(default=10, type='int'),
validate_certs=dict(required=False, default=False, type='bool')
),
supports_check_mode=True
)

if not ZabbixAPI:
module.fail_json(msg="Missing required zabbix-api module (check docs or install with: "
"pip install zabbix-api-erigones)")
raise AssertionError

server_url = module.params['server_url']
login_user = module.params['login_user']
login_password = module.params['login_password']
api_method = module.params['api_method']
api_params = module.params['api_params_list'] or module.params['api_params_dict']
timeout = module.params['timeout']
ssl_verify = module.params['validate_certs']

# login to zabbix
try:
# noinspection PyCallingNonCallable
zbx = ZabbixAPI(server=server_url, timeout=timeout, ssl_verify=ssl_verify)
zbx.login(login_user, login_password)
except Exception as e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
raise AssertionError

try:
result = zbx.call(api_method, api_params)
except ZabbixAPIException as e:
module.fail_json(msg="Zabbix API error: %s" % e)
raise AssertionError
except Exception as e:
module.fail_json(msg="Unknown failure: %s" % e)
raise AssertionError
else:
if api_method.endswith(('.get', '.isreadable', '.iswritable')):
changed = False
else:
changed = True

module.exit_json(changed=changed, result=result)


if __name__ == '__main__':
main()
9 changes: 8 additions & 1 deletion ans/library/zabbix_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# (c) 2012-2016, Erigones, s. r. o
# (c) 2012-2017, Erigones, s. r. o
# noinspection PyPep8Naming
import xml.etree.ElementTree as ET
import os
Expand Down Expand Up @@ -119,6 +119,7 @@ def indent(elem, level=0):
elem.tail = i


# noinspection PyShadowingBuiltins
def get_template_name(module, filename):
try:
with open(filename, 'r') as f:
Expand All @@ -132,6 +133,7 @@ def get_template_name(module, filename):
return data['zabbix_export']['templates'][0]['name']


# noinspection PyShadowingBuiltins
def get_template_id(module, zbx, name):
try:
result = zbx.template.get({
Expand All @@ -150,6 +152,7 @@ def get_template_id(module, zbx, name):
raise ValueError("Template \"%s\" not found" % name)


# noinspection PyShadowingBuiltins
def check_template(module, zbx, name):
try:
return bool(get_template_id(module, zbx, name))
Expand All @@ -159,6 +162,7 @@ def check_template(module, zbx, name):
module.fail_json(msg="Zabbix API problem: %s" % e)


# noinspection PyShadowingBuiltins
def import_template(module, zbx, filename, fmt):
if not os.path.exists(filename):
module.fail_json(msg="template file %s not found" % filename)
Expand Down Expand Up @@ -210,6 +214,7 @@ def import_template(module, zbx, filename, fmt):
raise AssertionError


# noinspection PyShadowingBuiltins
def export_template(module, zbx, template_id, target, fmt):
try:
result = zbx.configuration.export({
Expand Down Expand Up @@ -240,6 +245,7 @@ def export_template(module, zbx, template_id, target, fmt):
raise AssertionError


# noinspection PyShadowingBuiltins
def remove_template(module, zbx, template_id):
try:
return zbx.template.delete([template_id])
Expand All @@ -249,6 +255,7 @@ def remove_template(module, zbx, template_id):


def main():
# noinspection PyShadowingBuiltins
module = AnsibleModule(
argument_spec=dict(
server_url=dict(required=True, default=None, aliases=['url']),
Expand Down
9 changes: 5 additions & 4 deletions ans/upgrade/lib/runtasks.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
- name: upgrading to {{ tasklist|basename }}
- name: Upgrading to {{ tasklist|basename }}
set_fact:
upgrading_to: "{{ tasklist|basename }}"
upgrading_to: "{{ tasklist|basename }}"
current_task_dir: "{{ upg_dir }}/{{ tasklist|basename }}"

- include: "{{ upg_dir }}/{{ upgrading_to }}/main.yml"
- include: "{{ current_task_dir }}/main.yml"

- meta: flush_handlers

- name: bumping appliance version
- name: Bumping appliance version
shell: echo "{{ upgrading_to }}" > "{{ versionfile }}"
6 changes: 4 additions & 2 deletions ans/upgrade/lib/runupgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
upg_dir: "{{ upg_base }}/{{ appliance_type }}"
versionfile: /etc/esdc.version
tasks:
- name: extracting current appliance version
- name: Extracting current appliance version
shell: if [ -f "{{ versionfile }}" ]; then cat "{{ versionfile }}"; else echo 'v1.0.0'; fi
register: appliance_version
- name: getting list of upgrades
- name: Getting list of upgrades
local_action:
module: shell
args: find "{{ upg_dir }}" -maxdepth 1 -type d -name 'v*' | sort --version-sort
Expand All @@ -30,3 +30,5 @@
service: name=erigonesd-beat state=restarted
- name: reload haproxy
service: name=haproxy state=reloaded
- name: restart zabbix-server
service: name=zabbix-server state=restarted
21 changes: 21 additions & 0 deletions ans/upgrade/lib/zabbix_credentials.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# output is 3 lines:
# 1: zabbix api url
# 2: user
# 3: pass
- name: Query Zabbix credentials
local_action:
module: shell
args: python -c 'from vms.models import DefaultDc; print(DefaultDc().settings.MON_ZABBIX_SERVER); print(DefaultDc().settings.MON_ZABBIX_USERNAME); print(DefaultDc().settings.MON_ZABBIX_PASSWORD);'
environment:
ERIGONES_HOME: "{{ erigones_home }}"
PYTHONPATH: "{{ erigones_home }}:$PYTHONPATH"
VIRTUAL_ENV: "{{ erigones_home }}/envs"
PATH: "{{ erigones_home }}/bin:{{ erigones_home }}/envs/bin:$PATH"
DJANGO_SETTINGS_MODULE: "core.settings"
register: zabbix_credentials

- name: Set Zabbix credentials
set_fact:
zabbix_url: "{{ zabbix_credentials.stdout_lines[0] }}"
zabbix_login_user: "{{ zabbix_credentials.stdout_lines[1] }}"
zabbix_login_password: "{{ zabbix_credentials.stdout_lines[2] }}"
28 changes: 6 additions & 22 deletions ans/upgrade/lib/zabbix_import_templates.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
# This task searches for directory zabbix_templates in current task dir (e.g. v2.4.0/zabbix_templates)
# and imports all .json files that are present there.
# Required variables: upg_dir, upgrading_to
# Required variables: current_task_dir (=upg_dir/upgrading_to)
# Required tasks: zabbix_credentials.yml

- name: Preparing to update zabbix monitoring templates
set_fact:
tmpl_dir: '{{ upg_dir }}/{{ upgrading_to }}/files/zabbix_templates'
tmpl_dir: '{{ current_task_dir }}/files/zabbix_templates'

- name: Get list of new templates
local_action:
module: shell
args: find "{{ tmpl_dir }}" -name \*.json -type f -exec basename {} \;
register: tmpl_list

# output is 3 lines:
# 1: zabbix api url
# 2: user
# 3: pass
- name: Query Zabbix credentials
local_action:
module: shell
args: python -c 'from vms.models import DefaultDc; print(DefaultDc().settings.MON_ZABBIX_SERVER); print(DefaultDc().settings.MON_ZABBIX_USERNAME); print(DefaultDc().settings.MON_ZABBIX_PASSWORD);'
environment:
ERIGONES_HOME: "{{ erigones_home }}"
PYTHONPATH: "{{ erigones_home }}:$PYTHONPATH"
VIRTUAL_ENV: "{{ erigones_home }}/envs"
PATH: "{{ erigones_home }}/bin:{{ erigones_home }}/envs/bin:$PATH"
DJANGO_SETTINGS_MODULE: "core.settings"
register: zabbix_credentials

- name: Import monitoring templates
local_action:
module: zabbix_template
url="{{ zabbix_credentials.stdout_lines[0] }}"
login_user="{{ zabbix_credentials.stdout_lines[1] }}"
login_password="{{ zabbix_credentials.stdout_lines[2] }}"
url="{{ zabbix_url }}"
login_user="{{ zabbix_login_user }}"
login_password="{{ zabbix_login_password }}"
state=import
format=json
template="{{ tmpl_dir }}/{{ item }}"
with_items: "{{ tmpl_list.stdout.split('\n') }}"

6 changes: 3 additions & 3 deletions ans/upgrade/mgmt/v2.4.0/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
copy: dest=/etc/hostname content='localhost.localdomain\n' mode='u=rw,g=r,o=r'

- name: Update post-deploy script
copy: src="{{ upg_dir }}/{{ upgrading_to }}/files/50-es-post-deploy.sh" dest=/var/lib/rc-scripts/50-es-post-deploy.sh mode=0750 owner=root group=root
copy: src="{{ current_task_dir }}/files/50-es-post-deploy.sh" dest=/var/lib/rc-scripts/50-es-post-deploy.sh mode=0750 owner=root group=root

- name: Update post-deploy script unit file
copy: src="{{ upg_dir }}/{{ upgrading_to }}/files/rc-scripts.service" dest=/etc/systemd/system/rc-scripts.service
copy: src="{{ current_task_dir }}/files/rc-scripts.service" dest=/etc/systemd/system/rc-scripts.service
notify: reload systemd

- name: Update postgresql config
copy: src="{{ upg_dir }}/{{ upgrading_to }}/files/pg-ansible.conf" dest=/var/lib/pgsql/9.5/data/conf.d/ansible.conf
copy: src="{{ current_task_dir }}/files/pg-ansible.conf" dest=/var/lib/pgsql/9.5/data/conf.d/ansible.conf
notify:
- restart postgresql95
- restart erigonesd-beat
Expand Down
6 changes: 4 additions & 2 deletions ans/upgrade/mon/v2.3.1/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- name: Load Zabbix credentials
include: "{{ upg_base }}/lib/zabbix_credentials.yml"

- include: "{{ upg_base }}/lib/zabbix_import_templates.yml"

- name: Update fixed Zabbix templates
include: "{{ upg_base }}/lib/zabbix_import_templates.yml"
8 changes: 5 additions & 3 deletions ans/upgrade/mon/v2.4.0/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

- name: Delete hardcoded hostname from /etc/hosts
lineinfile: dest=/etc/hosts line="127.0.0.1 mon01" state=absent

- name: Update postgresql config
copy: src="{{ upg_dir }}/{{ upgrading_to }}/files/pg-ansible.conf" dest=/var/lib/pgsql/9.5/data/conf.d/ansible.conf
copy: src="{{ current_task_dir }}/files/pg-ansible.conf" dest=/var/lib/pgsql/9.5/data/conf.d/ansible.conf
notify: restart postgresql95

- include: "{{ upg_base }}/lib/zabbix_import_templates.yml"
- name: Load Zabbix credentials
include: "{{ upg_base }}/lib/zabbix_credentials.yml"

- name: Update fixed Zabbix templates
include: "{{ upg_base }}/lib/zabbix_import_templates.yml"
45 changes: 45 additions & 0 deletions ans/upgrade/mon/v2.5.2/files/alertscripts/hqsms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python

import sys
import requests

__USERNAME__ = '@USERNAME@'
__PASSWORD__ = '@PASSWORD@' # md5 hash
__FROM__ = '@FROM@'


def login_data():
"""
Login credentials in HQSMS service.
"""
return {
'username': __USERNAME__,
'password': __PASSWORD__,
'from': __FROM__,
}

def sms_send(phone, message):
"""
Send text message.
"""
data = login_data()
data['to'] = phone.replace('+', '')
data['message'] = message

return requests.post("https://ssl.hqsms.com/sms.do", data)


if __name__ == '__main__':
if len(sys.argv) < 3:
sys.stderr.write('Usage: %s <phone> <message>\n' % sys.argv[0])
sys.exit(1)

msg = str(' '.join(sys.argv[2:]))
r = sms_send(sys.argv[1], msg[:160])

print('%s (%s)' % (r.text, r.status_code))

if r.status_code == 200 and r.text.startswith('OK:'):
sys.exit(0)

sys.exit(1)
7 changes: 7 additions & 0 deletions ans/upgrade/mon/v2.5.2/files/alertscripts/jabber.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

JID=${1}
shift
MSG=$(echo "${@}" | sed 's/|\?\*UNKNOWN\*|\?//g')

curl -s -m 3 -o /dev/null -d "jid=${JID}&msg=${MSG}" http://127.0.0.1:8922/alert
7 changes: 7 additions & 0 deletions ans/upgrade/mon/v2.5.2/files/alertscripts/ludolph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

JID=${1}
shift
MSG=$(echo "${@}" | sed 's/|\?\*UNKNOWN\*|\?//g')

curl -s -m 3 -o /dev/null -d "jid=${JID}&msg=${MSG}" http://127.0.0.1:8922/alert
8 changes: 8 additions & 0 deletions ans/upgrade/mon/v2.5.2/files/alertscripts/sms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

PHONE="${1}" # Set this in user media
shift
# Everything else is the message itself
MSG=$(echo "${@}" | sed 's/|\?\*UNKNOWN\*|\?//g')

/etc/zabbix/alertscripts/hqsms.py "${PHONE}" "${MSG}"
17 changes: 17 additions & 0 deletions ans/upgrade/mon/v2.5.2/files/ludolph.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ludolph systemd service file
#
[Unit]
Description=Ludolph - Monitoring Jabber Bot
Documentation=https://github.com/erigones/Ludolph
After=network.target zabbix-server.service

[Service]
Type=simple
User=ludolph
ExecStart=/usr/bin/ludolph
Restart=on-failure
RestartPreventExitStatus=2
RestartSec=30

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit 782c6fe

Please sign in to comment.