forked from begood-technology/zabbix-twilio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zabbix-twilio.php
205 lines (169 loc) · 5.76 KB
/
zabbix-twilio.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
<?php
// **********************************************
// *** 外部ファイル ***
// **********************************************
// configファイル
require_once '/usr/lib/zabbix/alertscripts/zabbix-twilio/config.php';
// Twilio PHP ライブラリ
require_once $SCRIPT_DIR . '/lib/twilio-php-latest/Services/Twilio.php';
// Logger クラス
require_once $SCRIPT_DIR . '/Logger.php';
// **********************************************
// *** メイン ***
// **********************************************
$log = new Logger ( $LOG_DIR . '/' . basename($_SERVER['SCRIPT_FILENAME'], '.php') . '.log', $DEBUG_FLG );
$log->info ( 'Start' );
$log->debug ( $_REQUEST );
// コマンド指定確認
if (! isset ( $_REQUEST ['cmd'] )) {
$log->error ( '不正なリクエストです。' );
$log->error ( $_REQUEST );
exit ();
}
Switch ($_REQUEST ['cmd']) {
case "register" : // Zabbixへ登録
if (! isset ( $_REQUEST ['Digits'] )) {
$log->info ( "Digits 未入力" );
exit ();
}
// 入力キー確認
Switch ($_REQUEST ['Digits']) {
case $DIGITS :
$log->info ( 'Zabbixへ登録処理開始' );
$log->debug ( "ZABBIX_API:$ZABBIX_API ZABBIX_USER:$ZABBIX_USER ZABBIX_PASS:$ZABBIX_PASS" );
$log->debug ( "MESSAGE_REG_OK:$MESSAGE_REG_OK" );
$log->debug ( "MESSAGE_REG_NG:$MESSAGE_REG_NG" );
$response = new Services_Twilio_Twiml ();
try {
$api = new Zabbix_API ( $ZABBIX_API, $ZABBIX_USER, $ZABBIX_PASS );
$api_res = $api->request('event.acknowledge', array (
'eventids' => $_REQUEST ['eventid'],
'message' => $_REQUEST ['name'] . ' が受電確認済み。'
) );
// 登録成功
$response->say ( $MESSAGE_REG_OK, array (
'voice' => 'woman',
'language' => 'ja-JP'
) );
header ( 'Content-type: text/xml' );
print $response;
$log->info ( 'Zabbixへの登録成功' );
$log->debug ( $response );
} catch ( Exception $e ) {
// 登録失敗
$response->say ( $MESSAGE_REG_NG, array (
'voice' => 'woman',
'language' => 'ja-JP'
) );
header ( 'Content-type: text/xml' );
print $response;
$log->debug ( $response );
$log->error ( 'Zabbixへの登録失敗 ' );
$log->error ( $e->getMessage () );
return $e->getMessage ();
}
break;
case 0 :
case 1 < $_REQUEST ['Digits'] and $_REQUEST ['Digits'] < 10 :
$log->info ( "Digits 不一致 Digits:$_REQUEST ['Digits]" );
$log->info ( "再入力処理開始" );
$log->debug ( "MESSAGE_RETYPE:$MESSAGE_RETYPE" );
$response = new Services_Twilio_Twiml ();
$gather = $response->gather ( array (
'action' => $SCRIPT_URL . '?cmd=register&eventid=' . $_REQUEST ['eventid'] . '&name=' . $_REQUEST ['name'],
'timeout' => $DIGITS_TIMEOUT,
'finishOnKey' => '#',
'numDigits' => $DIGITS_NUM
) );
// $$DIGITS$$を置き換え
$message = str_replace ( '$$DIGITS$$', $DIGITS, $MESSAGE_RETYPE );
$gather->say ( $message, array (
'voice' => 'woman',
'language' => 'ja-JP'
) );
header ( 'Content-type: text/xml' );
print $response;
$log->debug ( $response );
break;
}
break;
case "notice" : // アラート通知
$log->info ( "自動通知処理開始" );
// URLデコード
$message = urldecode ( $_REQUEST ['message'] );
// $$MESSAGE$$
$message = str_replace ( '$$MESSAGE$$', $message, $MESSAGE_NOTICE );
// $$DIGITS$$を置き換え
$message = str_replace ( '$$DIGITS$$', $DIGITS, $message );
$log->debug ( "MESSAGE:$message" );
$log->debug ( "DIGITS_TIMEOUT:$DIGITS_TIMEOUT DIGITS_NUM:$DIGITS_NUM URL:$SCRIPT_URL" );
$response = new Services_Twilio_Twiml ();
$gather = $response->gather ( array (
'action' => $SCRIPT_URL . '?cmd=register&eventid=' . $_REQUEST ['eventid'] . '&name=' . $_REQUEST ['name'],
'timeout' => $DIGITS_TIMEOUT,
'finishOnKey' => '#',
'numDigits' => $DIGITS_NUM
) );
$gather->say ( $message, array (
'voice' => 'woman',
'language' => 'ja-JP'
) );
header ( 'Content-type: text/xml' );
print $response;
$log->debug ( $response );
break;
default :
}
exit ();
// ******************************************************************
// *** Zabbix_API ***
// ******************************************************************
class Zabbix_API {
private $api_url = '';
private $auth = '';
public function __construct($api_url, $user='', $password='') {
$this->api_url = $api_url;
if (!empty($user) and !empty($password)) {
$this->userLogin(array('user' => $user, 'password' => $password));
}
}
public function userLogin($params) {
$res = $this->request('user.login', $params);
$this->auth = $res['result'];
}
public function request($method, $params) {
// APIをJSONエンコード
$content = json_encode( array (
'jsonrpc' => '2.0',
'method' => $method,
'params' => $params,
'auth' => $this->auth,
'id' => '1'
));
// リクエストオプション
$context = stream_context_create(array('http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json-rpc; charset=UTF-8' . "\r\n",
'content' => $content,
'ignore_errors' => true
)));
// リクエスト
$res = @file_get_contents ( $this->api_url, false, $context);
if (!$res) {
throw new Exception('"' . $this->api_url . '"' . 'からデータを取得できません。');
}
// JSONデコード
$api_res = json_decode($res, true);
if (!$api_res) {
$msg = print_r($res, true);
throw new Exception('レスポンスデータがJSON形式ではありません。' . "\n\n" . $msg);
}
// APIの返り値チェック
if (array_key_exists('error', $api_res)) {
$msg = print_r($api_res, true);
throw new Exception('API エラーが発生しました。' . "\n\n" . $msg);
}
return $api_res;
}
}
?>