/
library.php
211 lines (166 loc) · 5.11 KB
/
library.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
require_once dirname ( __FILE__ ).'/MysqliDb.php';
global $config;
$config = parse_ini_file('config.inc', true);
$log = '/usr/lib/zabbix/alertscripts/zabbix-reactio/reactio.log';
function getReactioIncidentIdByEventId($eventid) {
global $config;
if (empty($eventid)) {
return;
}
$db_info = $config['db_info'];
$db = new Mysqlidb ($db_info['host'], $db_info['user'], $db_info['pass'], $db_info['db']);
$db->where ("eventid", $eventid);
$res = $db->getOne('alerts', 'reactio_incident_id');
$reactio_incident_id = $res['reactio_incident_id'];
return $reactio_incident_id;
}
function updateReactioIncidentIdByEventId($eventid, $reactio_incident_id) {
global $config;
$is_success = false;
if (empty($eventid)) {
return $is_success;
}
if (empty($reactio_incident_id)) {
return $is_success;
}
$db_info = $config['db_info'];
$db = new Mysqlidb ($db_info['host'], $db_info['user'], $db_info['pass'], $db_info['db']);
$db->where ('eventid', $eventid);
$data['reactio_incident_id'] = $reactio_incident_id;
if ($db->update ('alerts', $data)) {
$is_success = true;
}
return $is_success;
}
/**
* Zabbix 障害検知のサブジェクトよりステータス状況取得
*
* @param $subject string - Zabbix subject
* @return string - Reatio status of Incident
*/
function getStatusBySubject($subject) {
if (empty($subject)) {
return;
}
if (preg_match('/RECOVERY/', $subject)) {
return 'close';
} else if ( preg_match('/PROBLEM/', $subject) ) {
return 'open';
}
return;
}
/**
* Zabbix 障害検知メッセージよりイベントID取得
*
* @param $message string - Zabbix message
* @return int - Zabbix EVENT ID
*/
function getEventIdByMessage($message) {
if (empty($message)) {
return;
}
preg_match('/EVENT_ID: (?P<eventid>\d+)/', $message, $match);
if (!empty($match['eventid'])) {
return $match['eventid'];
}
return;
}
/**
* Zabbix 障害検知サブジェクトよりApiKey取得
*
* @param $subject string - Zabbix subject
* @param $project string - Zabbix HOSTNAME
* @return string - Reactio API KEY
*/
function getApiKeyBySubject($subject) {
global $config;
if (empty($subject)) {
return;
}
if (empty($config['api_key'])) {
return;
}
$api_key = '';
foreach($config['api_key'] as $host => $key) {
$pattern = '/'.$host.'/';
if (preg_match($pattern, $subject)) {
$api_key = $key;
break;
}
}
return $api_key;
}
/**
* インシデント作成
*
* @param $api_key string - Reactio API KEY
* @param $status integer - Incident status
* @param $message string - Incident message
* @return json - Reactio API response JSON
*/
function createIncident($api_key, $status, $subject, $message) {
global $config;
if (empty($config['reactio_url']['default'])) {
return;
}
$url = $config['reactio_url']['default'];
$post_data = array();
$post_data['name'] = $subject;
$post_data['status'] = $status;
$post_data['topics'] = ["原因調査","復旧作業"];
$post_data['notification_text'] = $message;
$post_data['notification_call'] = true;
$post_data['message'] = '原因調査を実施お願いします';
$result = curlPost($api_key, $url, $post_data);
return $result;
}
/**
* インシデント作成
*
* @param $api_key string - Reactio API KEY
* @param $status integer - Incident status
* @param $incident_id integer - Incident ID
* @return json - Reactio API response JSON
*/
function updateStatusIncident($api_key, $status, $incident_id) {
global $config;
if (empty($config['reactio_url']['default'])) {
return;
}
$url = sprintf( '%s/%s/status', $config['reactio_url']['default'], $incident_id);
$post_data = array();
$post_data['status'] = $status;
$result = curlPost($api_key, $url, $post_data);
return $result;
}
/**
* curl による POST
* @param $api_key string - API KEY
* @param $url string - URL
* @param $post_data array - POST DATA
* @return json - Reactio API response JSON
*/
function curlPost($api_key, $url, $post_data) {
$header = array(
"Accept: application/json",
"Content-type: application/json",
"X-Api-Key: {$api_key}"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$result = json_decode($body, true);
curl_close($curl);
return $result;
}
?>