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+ }
0 commit comments