-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessLogstash.module.php
126 lines (96 loc) · 3.58 KB
/
ProcessLogstash.module.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
<?php namespace ProcessWire;
class ProcessLogstash extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return array(
'title' => 'ProcessLogstash',
'class' => 'ProcessLogstash',
'version' => 106,
'summary' => 'Send ProcessWire Logs to Logstash/Kibana',
'href' => 'https://github.com/blue-tomato/ProcessLogstash/',
'singular' => true,
'autoload' => true,
'requires' => [
'PHP>=7.0.0',
'ProcessWire>=3.0.133',
'InputfieldURL>=1.0.1'
]
);
}
public function init()
{
$this->addHookAfter('WireLog::save', $this, 'sendToLogstash');
}
private $processLogstashFieldname = 'processlogstash_endpoint';
public function getModuleConfigInputfields(InputfieldWrapper $wrapper)
{
$endpoint = $this->getEndpoint();
$field = $this->modules->InputfieldURL;
$field->name = $this->processLogstashFieldname;
$field->required = true;
$field->label = __("Logstash HTTP-Input Endpoint");
$field->value = isset($endpoint) ? $endpoint : '';
$wrapper->add($field);
return $wrapper;
}
private function getEndpoint() {
$config = Wire::getFuel('config');
if(isset($config->processLogstash['endpoint'])) {
return $config->processLogstash['endpoint'];
}
return $this->get($this->processLogstashFieldname);
}
public function sendToLogstash(HookEvent $event)
{
$users = Wire::getFuel('users');
$input = Wire::getFuel('input');
$config = Wire::getFuel('config');
$name = $event->arguments(0);
$text = $event->arguments(1);
$options = $event->arguments(2);
$user = $users->getCurrentUser()->name;
$url = $input->url();
if($options && isset($options["user"])) $user = $options["user"];
if($options && isset($options["url"])) $url = $options["url"];
$logData = [
"timestamp" => date('c'),
"logType" => $name,
"user" => $user,
"url" => $url,
"text" => $text
];
if(isset($config->processLogstash['env'])) {
$logData["env"] = $config->processLogstash['env'];
}
$response = $this->sendData($logData);
}
private function sendData(array $data)
{
$response = null;
$endpoint = $this->getEndpoint();
if($endpoint) {
$config = Wire::getFuel('config');
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'LogType: ' . $data['logType']
),
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)
);
if(isset($config->processLogstash['customHttpHeaders']) && is_array($config->processLogstash['customHttpHeaders'])) {
$curlConfig[CURLOPT_HTTPHEADER] = array_merge($curlConfig[CURLOPT_HTTPHEADER], $config->processLogstash['customHttpHeaders']);
}
if(isset($config->processLogstash['proxy'])) {
$curlConfig[CURLOPT_PROXY] = $config->processLogstash['proxy'];
}
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
}
return $response; // TODO: json_decode if needed
}
}