Skip to content

Commit 5e4cfea

Browse files
committed
Initial version as POC
1 parent 5d777da commit 5e4cfea

File tree

4 files changed

+287
-0
lines changed

4 files changed

+287
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 the Coding Company
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MariaDBStore.php

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
/**
3+
* Intellectual Property of Svensk Coding Company AB - Sweden All rights reserved.
4+
*
5+
* @copyright (c) 2016, Svensk Coding Company AB
6+
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
7+
* @version 1.0
8+
* @license http://www.apache.org/licenses/GPL-compatibility.html GPL
9+
*
10+
*/
11+
namespace theCodingCompany;
12+
13+
use PDO;
14+
15+
/**
16+
* Class to route FluentD output to MariaDB store
17+
*/
18+
class MariaDBStore
19+
{
20+
/**
21+
* MariaDB PDO connection
22+
* @var type
23+
*/
24+
protected $db_con = null;
25+
26+
/**
27+
* Settings as key value array ["cdn" => "", "username" => "", "password" => ""]
28+
* @var array
29+
*/
30+
private $settings = [];
31+
32+
/**
33+
* The name of the database tabel where to store the data
34+
* @var string
35+
*/
36+
private $db_table = null;
37+
38+
/**
39+
* The fieldnames we use for the insert
40+
* @var array
41+
*/
42+
private $db_fields = [];
43+
44+
/**
45+
* The values to insert
46+
* @var array
47+
*/
48+
private $db_values = [];
49+
50+
/**
51+
* Construct new Class
52+
*/
53+
public function __construct($settings = [])
54+
{
55+
$this->settings = $settings;
56+
$this->DBConnect();
57+
}
58+
59+
/**
60+
* Start the reader
61+
*/
62+
public function start()
63+
{
64+
$this->log(date("Ymd H:i:s"). " MariaDB Storage running......");
65+
while(true)
66+
{
67+
$line = trim(fgets(STDIN));
68+
if($line !== ""){
69+
70+
$this->log($line);
71+
$this->getValues($line)
72+
->insert();
73+
}
74+
}
75+
$this->log(date("Ymd H:i:s"). " MariaDB Storage stopped");
76+
}
77+
78+
/**
79+
* Destruct
80+
*/
81+
public function __destruct()
82+
{
83+
$this->log(date("Ymd H:i:s"). " MariaDB Storage stopped");
84+
}
85+
86+
/**
87+
* Get the values to insert
88+
* @param string $json
89+
* @return void
90+
*/
91+
private function getValues($json = null)
92+
{
93+
$this->db_values = []; //Reset to nothing
94+
95+
if(($assoc = json_decode($json, true)) !== NULL){
96+
97+
foreach($this->db_fields as $field){
98+
if(isset($assoc[$field])){
99+
array_push($this->db_values, json_encode($assoc[$field]));
100+
}
101+
}
102+
103+
}else{
104+
$this->log("Cant decode JSON:\r\n " . print_r($json, true) . "\r\n");
105+
}
106+
return $this;
107+
}
108+
109+
/**
110+
* Connect to the MariaDB database
111+
*/
112+
private function DBConnect()
113+
{
114+
/**
115+
* Connect to the database
116+
*/
117+
try
118+
{
119+
$this->db_con = new PDO(
120+
$this->settings["cdn"],
121+
$this->settings["username"],
122+
$this->settings["password"], [
123+
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
124+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
125+
PDO::ATTR_AUTOCOMMIT => TRUE
126+
]
127+
);
128+
129+
}
130+
catch(\Exception $err)
131+
{
132+
$this->log($err->getMessage());
133+
$this->log(print_r($err->getTraceAsString(), true));
134+
}
135+
}
136+
137+
/**
138+
* Set the table for our insert statement
139+
* @param string $tablename
140+
* @return \Fluentd\MariaDBStore
141+
*/
142+
public function setTable($tablename = "")
143+
{
144+
$this->db_table = $tablename;
145+
146+
return $this;
147+
}
148+
149+
/**
150+
* Set the table fieldnames
151+
* @param array $fields
152+
* @return \Fluentd\MariaDBStore
153+
*/
154+
public function setFields($fields = [])
155+
{
156+
$this->db_fields = $fields;
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* Insert data into the table
163+
* @return boolean
164+
*/
165+
public function insert()
166+
{
167+
$sql = $this->createInsertSQL();
168+
169+
try
170+
{
171+
$stmt = $this->db_con->prepare($sql);
172+
if($stmt->execute() === FALSE){
173+
$this->log(print_r($this->db_con->errorInfo(), true));
174+
}
175+
}
176+
catch(\Exception $err)
177+
{
178+
$this->log($err->getMessage());
179+
$this->log(print_r($err->getTraceAsString(), true));
180+
}
181+
182+
return true;
183+
}
184+
185+
/**
186+
* Create the SQL insert statement
187+
* @todo Needs rework. Didn't find it important enough yet. Feel free to do so.
188+
* @return string
189+
*/
190+
private function createInsertSQL()
191+
{
192+
$field_data = "";
193+
foreach($this->db_fields as $field){
194+
$field_data .= "`{$field}`,";
195+
}
196+
$field_data = substr($field_data, 0, -1);
197+
198+
$value_data = "";
199+
foreach($this->db_values as $val){
200+
$value_data .= "{$this->db_con->quote($val)},";
201+
}
202+
$value_data = substr($value_data, 0, -1);
203+
204+
return "INSERT INTO `{$this->db_table}` ({$field_data}) VALUES({$value_data})";
205+
}
206+
207+
/**
208+
* Log info to file
209+
* @param type $string
210+
*/
211+
private function log($string = "")
212+
{
213+
syslog(LOG_INFO, $string);
214+
215+
file_put_contents("/var/log/fluentd_log", $string."\r\n", FILE_APPEND);
216+
}
217+
}

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "thecodingcompany/php-fluentd-mariadb",
3+
"type": "library",
4+
"description": "PHP Library to use with Fluentd exec_filter plugin",
5+
"keywords": ["php","fluentd","exec_filter","mariadb"],
6+
"homepage": "https://github.com/TheCodingCompany/php-fluentd-mariadb",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Victor Angelier",
11+
"email": "vangelier@hotmail.com",
12+
"homepage": "https://www.thecodingcompany.se",
13+
"role": "Founder"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.6.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"theCodingCompany\\": "theCodingCompany/"
22+
}
23+
}
24+
}

example.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Intellectual Property of Svensk Coding Company AB - Sweden All rights reserved.
4+
*
5+
* @copyright (c) 2016, Svensk Coding Company AB
6+
* @author V.A. (Victor) Angelier <victor@thecodingcompany.se>
7+
* @version 1.0
8+
* @license http://www.apache.org/licenses/GPL-compatibility.html GPL
9+
*
10+
*/
11+
12+
require_once("theCodingCompany/MariaDBStore.php");
13+
14+
$settings = array(
15+
"cdn" => "mysql:host=127.0.0.1;port=3306;dbname=event_log;",
16+
"username" => "root",
17+
"password" => "SuperSecretPassword"
18+
);
19+
20+
$fields = array("attributes");
21+
22+
$maria = new Fluentd\MariaDBStore($settings);
23+
$maria->setTable("event_log")
24+
->setFields($fields)
25+
->start();

0 commit comments

Comments
 (0)