-
Notifications
You must be signed in to change notification settings - Fork 0
/
MariaDBStore.php
217 lines (189 loc) · 4.99 KB
/
MariaDBStore.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
212
213
214
215
216
217
<?php
/**
* Intellectual Property of Svensk Coding Company AB - Sweden All rights reserved.
*
* @copyright (c) 2016, Svensk Coding Company AB
* @author V.A. (Victor) Angelier <[email protected]>
* @version 1.0
* @license http://www.apache.org/licenses/GPL-compatibility.html GPL
*
*/
namespace theCodingCompany;
use PDO;
/**
* Class to route FluentD output to MariaDB store
*/
class MariaDBStore
{
/**
* MariaDB PDO connection
* @var type
*/
protected $db_con = null;
/**
* Settings as key value array ["cdn" => "", "username" => "", "password" => ""]
* @var array
*/
private $settings = [];
/**
* The name of the database tabel where to store the data
* @var string
*/
private $db_table = null;
/**
* The fieldnames we use for the insert
* @var array
*/
private $db_fields = [];
/**
* The values to insert
* @var array
*/
private $db_values = [];
/**
* Construct new Class
*/
public function __construct($settings = [])
{
$this->settings = $settings;
$this->DBConnect();
}
/**
* Start the reader
*/
public function start()
{
$this->log(date("Ymd H:i:s"). " MariaDB Storage running......");
while(true)
{
$line = trim(fgets(STDIN));
if($line !== ""){
$this->log($line);
$this->getValues($line)
->insert();
}
}
$this->log(date("Ymd H:i:s"). " MariaDB Storage stopped");
}
/**
* Destruct
*/
public function __destruct()
{
$this->log(date("Ymd H:i:s"). " MariaDB Storage stopped");
}
/**
* Get the values to insert
* @param string $json
* @return void
*/
private function getValues($json = null)
{
$this->db_values = []; //Reset to nothing
if(($assoc = json_decode($json, true)) !== NULL){
foreach($this->db_fields as $field){
if(isset($assoc[$field])){
array_push($this->db_values, json_encode($assoc[$field]));
}
}
}else{
$this->log("Cant decode JSON:\r\n " . print_r($json, true) . "\r\n");
}
return $this;
}
/**
* Connect to the MariaDB database
*/
private function DBConnect()
{
/**
* Connect to the database
*/
try
{
$this->db_con = new PDO(
$this->settings["cdn"],
$this->settings["username"],
$this->settings["password"], [
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_AUTOCOMMIT => TRUE
]
);
}
catch(\Exception $err)
{
$this->log($err->getMessage());
$this->log(print_r($err->getTraceAsString(), true));
}
}
/**
* Set the table for our insert statement
* @param string $tablename
* @return \Fluentd\MariaDBStore
*/
public function setTable($tablename = "")
{
$this->db_table = $tablename;
return $this;
}
/**
* Set the table fieldnames
* @param array $fields
* @return \Fluentd\MariaDBStore
*/
public function setFields($fields = [])
{
$this->db_fields = $fields;
return $this;
}
/**
* Insert data into the table
* @return boolean
*/
public function insert()
{
$sql = $this->createInsertSQL();
try
{
$stmt = $this->db_con->prepare($sql);
if($stmt->execute() === FALSE){
$this->log(print_r($this->db_con->errorInfo(), true));
}
}
catch(\Exception $err)
{
$this->log($err->getMessage());
$this->log(print_r($err->getTraceAsString(), true));
}
return true;
}
/**
* Create the SQL insert statement
* @todo Needs rework. Didn't find it important enough yet. Feel free to do so.
* @return string
*/
private function createInsertSQL()
{
$field_data = "";
foreach($this->db_fields as $field){
$field_data .= "`{$field}`,";
}
$field_data = substr($field_data, 0, -1);
$value_data = "";
foreach($this->db_values as $val){
$value_data .= "{$this->db_con->quote($val)},";
}
$value_data = substr($value_data, 0, -1);
return "INSERT INTO `{$this->db_table}` ({$field_data}) VALUES({$value_data})";
}
/**
* Log info to file
* @param type $string
*/
private function log($string = "")
{
syslog(LOG_INFO, $string);
file_put_contents("/var/log/fluentd_log", $string."\r\n", FILE_APPEND);
}
}