-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-zabbix-agents.yml
171 lines (141 loc) · 5.05 KB
/
configure-zabbix-agents.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
# configure-zabbix-agents.yml
- name: configure nodes for docker monitoring in zabbix
hosts: nodes
gather_facts: no
remote_user: ec2_user
become: yes
become_user: root
handlers:
- name: restart zabbix agent
service:
name: zabbix-agent
state: restarted
tasks:
- name: sudoers for Zabbix
copy:
dest: /etc/sudoers.d/zabbix
content: |
Defaults:zabbix visiblepw
Defaults:zabbix !requiretty
zabbix ALL=(ALL) NOPASSWD: /bin/docker ps *
zabbix ALL=(ALL) NOPASSWD: /sbin/lvs *
- name: check if zabbix_agent_t is permissive
shell: semanage permissive -l|grep zabbix_agent_t
ignore_errors: yes
register: permissive
changed_when: false
- name: Set zabbix agent permissive in SElinux
command: semanage permissive -a zabbix_agent_t
when: permissive|failed
- name: Create userParameter for key docker.ps[]
copy:
dest: /etc/zabbix/zabbix_agentd.d/docker.conf
content: |
UserParameter=docker.ps[*],sudo docker ps -q -f $1|wc -l
notify: restart zabbix agent
- name: Create userParameter for lvm.data[]
copy:
dest: /etc/zabbix/zabbix_agentd.d/lvm.conf
content: |
UserParameter=lvm.data[*],LVM_SUPPRESS_FD_WARNINGS=1 sudo lvs --no-heading -o lv_name,$2|awk '$$1 == "$1" { print $$2 }'
notify: restart zabbix agent
- name: Create userParameter for openshift_node.requests[]
copy:
dest: /etc/zabbix/zabbix_agentd.d/requests.conf
content: |
UserParameter=openshift_node.requests[*],/opt/monitoring/requests.sh $1
notify: restart zabbix agent
- name: install bc
yum:
name: bc
state: present
- name: configure zabbix SA and fetch TOKEN and URL
hosts: masters[0]
tasks:
- name: check if Zabbix SA exists
command: oc get sa zabbix -n default
register: sacommand
ignore_errors: yes
changed_when: false
- name: create Zabbix SA
command: oc create sa zabbix -n default
when: sacommand | failed
- name: add policy for zabbix SA
command: oc adm policy add-cluster-role-to-user cluster-reader system:serviceaccount:default:zabbix -n default
when: sacommand | failed
- name: get token (step 1)
shell: "{% raw %}oc get sa/zabbix -n default --template='{{range .secrets}}{{ .name }} {{end}}' | xargs -n 1 oc -n default get secret --template='{{ if .data.token }}{{ .data.token }}{{end}}' | head -n 1 | base64 -d - > /tmp/zabbix.token{% endraw %}"
changed_when: false
# cannot use register directly in step 1 as there seem to be a bug in ansible
# this workaround works
- name: get token (step 2)
command: cat /tmp/zabbix.token
register: token
- name: remove temp file
file:
path: /tmp/zabbix.token
state: absent
- name: get Hawkular route
command: "oc get route/hawkular-metrics -n openshift-infra --template='{% raw %}{{ .spec.host }}{% endraw %}'"
register: hawkular
changed_when: false
- name: set facts for hawkular and token zabbix
set_fact:
token: "{{ token.stdout }}"
hawkular: "{{ hawkular.stdout }}"
when:
- token | succeeded
- hawkular | succeeded
- name: configure nodes for docker monitoring in zabbix
hosts: nodes
gather_facts: no
vars:
token: "{{ hostvars[groups['masters'][0]].token }}"
hawkular: "{{ hostvars[groups['masters'][0]].hawkular }}"
tasks:
- name: remove /etc/zabbix/request.rc (renamed requests.rc)
file:
path: /etc/zabbix/request.rc
state: absent
- name: create config file for requests.sh script
copy:
dest: /etc/zabbix/requests.rc
content: |
TOKEN={{ token}}
HAWKULAR={{ hawkular}}
owner: zabbix
mode: 0600
- name: create /opt/monitoring
file:
path: /opt/monitoring
state: directory
- name: remove request.sh (renamed requests.sh)
file:
path: /opt/monitoring/request.sh
state: absent
- name: create requests.sh
copy:
dest: /opt/monitoring/requests.sh
mode: 0755
owner: root
content: |
#!/bin/bash
set -euo pipefail
# example usage
# ./requests.sh memory
# ./requests.sh cpu
# args:
# - cpu|memory
#
# output:
# returns the % request/node_capacity, (also visible in `oc describe node`)
WHAT=$1
# load configuration (TOKEN and HAWKULAR host)
. /etc/zabbix/requests.rc
CURLOPT=(-s -k -H "Authorization: Bearer ${TOKEN}" -H 'Hawkular-Tenant: _system' -X GET)
URL="https://${HAWKULAR}/hawkular/metrics/gauges/data?tags=group_id:/${WHAT}/request,hostname:$(hostname)&bucketDuration=1h&limit=1"
request=$(curl "${CURLOPT[@]}" "$URL" | python -m json.tool|grep avg|head -1|sed 's/^ *"avg *"://;s/,$//')
URL="https://${HAWKULAR}/hawkular/metrics/gauges/data?tags=group_id:/${WHAT}/node_capacity,hostname:$(hostname)&bucketDuration=1h&limit=1"
capacity=$(curl "${CURLOPT[@]}" "$URL" | python -m json.tool|grep avg|head -1|sed 's/^ *"avg *"://;s/,$//')
bc <<< "scale=2; 100*${request}/${capacity}"