-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
78 lines (65 loc) · 2.56 KB
/
index.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
<?php
require __DIR__ . '/vendor/autoload.php';
require 'TelegramMessage.php';
use RingCentral\Psr7\Response;
/*
To enable the initializer feature (https://help.aliyun.com/document_detail/89029.html)
please implement the initializer function as below:
function initializer($context) {
echo 'initializing' . PHP_EOL;
}
*/
function handler($request, $context): Response{
$body = $request->getBody()->getContents();
$queries = $request->getQueryParams();
// $method = $request->getMethod();
$headers = $request->getHeaders();
// $path = $request->getAttribute('path');
// $requestURI = $request->getAttribute('requestURI');
// $clientIP = $request->getAttribute('clientIP');
$data = null;
$logger = $GLOBALS['fcLogger'];
$prefix = getenv('PREFIX') ?: '`[CM]` ';
$logger->info(json_encode([$headers, $body]));
// Signature verification
if (isset($queries['signature']) && getenv('SIGNATURE') !== $queries['signature']) {
return new Response(200, [], 'OK');
}
// Threshold alarm
if (strpos($headers['Content-Type'][0], 'application/x-www-form-urlencoded') !== false)
{
parse_str($body, $data);
$required_fields = ['alertName', 'alertState', 'curValue', 'instanceName', 'metricName'];
if (validate_request($data, $required_fields)) {
$message = "{$prefix}*{$data['alertName']} {$data['metricName']}* for `{$data['instanceName']}` is `{$data['alertState']}`. Value: {$data['curValue']}";
}
}
// Event alarm
elseif (strpos($headers['Content-Type'][0], 'application/json') !== false) {
$data = json_decode($body, true);
$required_fields = ['product', 'level', 'instanceName', 'name'];
if (validate_request($data, $required_fields)) {
$message = "{$prefix}*{$data['content']['instanceIds'][0]}* - `{$data['name']}`\n{$data['content']['description']}";
}
} else {
$message = "{$prefix}Invalid request _content-type_.";
}
// Send message to Telegram
send_message($message);
return new Response(200, [], 'OK');
}
function validate_request($data, $required_fields)
{
foreach ($required_fields as $field)
{
if (!isset($data[$field])) {
send_message("{$prefix}*ERROR*: Required field {$field} is missing.");
return false;
}
}
return true;
}
function send_message($message)
{
TelegramMessage::send(getenv('TG_API_KEY'), getenv('TG_CHAT_ID'), $message, false);
}