hlit-mon  0.5.2
 All Classes Functions Groups Pages
Writer Class Reference

This сlass interacts with the database. More...

#include <Writer.h>

Public Member Functions

bool Connect_DB ()
 
void Create_Tables ()
 
void Clear_Tables ()
 
void Write (map< pair< string, string >, map< string, double > > map_journal)
 
void Write_Day ()
 
void Write_Month ()
 
void Set_Server (char *serv)
 
void Set_User (char *usr)
 
void Set_Password (char *psd)
 
void Set_Database (char *db)
 
void Set_Off_Write_Day ()
 
void Set_Off_Write_Month ()
 
void Delete_Old_Records ()
 
void Check_Connection ()
 

Detailed Description

This сlass interacts with the database.

Author
Mayorov

Receives data from 'Keeper' and writes them to the database. Calculates average load per day/month and records in the database. Cleans up obsolete entries.

Definition at line 24 of file Writer.h.

Member Function Documentation

void Writer::Check_Connection ( )

Checks the connection. If it is broken, connect again.

Definition at line 79 of file Writer.cpp.

80 {
81  if(mysql_ping(writer) > 0)
82  {
83  if (!mysql_real_connect(writer, server, user, password, database, 0, NULL, 0)) {
84  fprintf(stderr, "%s\n", mysql_error(writer));
85  }
86  }
87 
88  if(mysql_ping(reader) > 0)
89  {
90  if (!mysql_real_connect(reader, server, user, password, database, 0, NULL, 0)) {
91  fprintf(stderr, "%s\n", mysql_error(reader));
92  }
93  }
94 }
void Writer::Clear_Tables ( )

Removes all data from a tables 'cluster_usage_hourly', 'cluster_usage_daily', 'cluster_usage_monthly'

Definition at line 116 of file Writer.cpp.

117 {
118  if (mysql_query(writer, "TRUNCATE TABLE cluster_usage_hourly")) {
119  fprintf(stderr, "%s\n", mysql_error(writer));
120  exit(1);
121  }
122 
123  if (mysql_query(writer, "TRUNCATE TABLE cluster_usage_daily")) {
124  fprintf(stderr, "%s\n", mysql_error(writer));
125  exit(1);
126  }
127 
128  if (mysql_query(writer, "TRUNCATE TABLE cluster_usage_monthly")) {
129  fprintf(stderr, "%s\n", mysql_error(writer));
130  exit(1);
131  }
132 
133  printf("Tables are cleared\n");
134 }
bool Writer::Connect_DB ( )

Establishes connection with database.

Returns
Returns success of connection

Definition at line 60 of file Writer.cpp.

61 {
62  writer = mysql_init(NULL);
63  reader = mysql_init(NULL);
64 
65  if (!mysql_real_connect(writer, server, user, password, database, 0, NULL, 0)) {
66  fprintf(stderr, "%s\n", mysql_error(writer));
67  return false;
68  }
69 
70  if (!mysql_real_connect(reader, server, user, password, database, 0, NULL, 0)) {
71  fprintf(stderr, "%s\n", mysql_error(reader));
72  return false;
73  }
74 
75  printf("Сonnection to database is established\n");
76  return true;
77 }
void Writer::Create_Tables ( )

Creates three tables in database: 'cluster_usage_hourly', 'cluster_usage_daily', 'cluster_usage_monthly'

Definition at line 96 of file Writer.cpp.

97 {
98  if (mysql_query(writer, "CREATE TABLE cluster_usage_hourly (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, type varchar(50), nodename varchar (100) NOT NULL, time BIGINT(15), cpu_user DOUBLE (50, 2) NOT NULL, cpu_sys DOUBLE (50, 2) NOT NULL, mem_total DOUBLE (50, 2) NOT NULL, mem_used DOUBLE (50, 2) NOT NULL, mem_buffer DOUBLE (50, 2) NOT NULL, mem_cached DOUBLE (50, 2) NOT NULL, net_in DOUBLE (50, 2) NOT NULL, net_out DOUBLE (50, 2) NOT NULL)")) {
99  fprintf(stderr, "%s\n", mysql_error(writer));
100  exit(1);
101  }
102 
103  if (mysql_query(writer, "CREATE TABLE cluster_usage_daily (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, type varchar(50), nodename varchar (100) NOT NULL, time BIGINT(15), cpu_user DOUBLE (50, 2) NOT NULL, cpu_sys DOUBLE (50, 2) NOT NULL, mem_total DOUBLE (50, 2) NOT NULL, mem_used DOUBLE (50, 2) NOT NULL, mem_buffer DOUBLE (50, 2) NOT NULL, mem_cached DOUBLE (50, 2) NOT NULL, net_in DOUBLE (50, 2) NOT NULL, net_out DOUBLE (50, 2) NOT NULL)")) {
104  fprintf(stderr, "%s\n", mysql_error(writer));
105  exit(1);
106  }
107 
108  if (mysql_query(writer, "CREATE TABLE cluster_usage_monthly (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, type varchar(50), nodename varchar (100) NOT NULL, time BIGINT(15), cpu_user DOUBLE (50, 2) NOT NULL, cpu_sys DOUBLE (50, 2) NOT NULL, mem_total DOUBLE (50, 2) NOT NULL, mem_used DOUBLE (50, 2) NOT NULL, mem_buffer DOUBLE (50, 2) NOT NULL, mem_cached DOUBLE (50, 2) NOT NULL, net_in DOUBLE (50, 2) NOT NULL, net_out DOUBLE (50, 2) NOT NULL)")) {
109  fprintf(stderr, "%s\n", mysql_error(writer));
110  exit(1);
111  }
112 
113  printf("Tables are created\n");
114 }
void Writer::Delete_Old_Records ( )

Removes obsolete entries from tables. For 'cluster_usage_hourly' older than 1 year. For 'cluster_usage_daily' older than 3 years. For 'cluster_usage_monthly' older than 5 years.

Definition at line 238 of file Writer.cpp.

239 {
240  if (mysql_query(writer, "DELETE FROM cluster_usage_hourly WHERE time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)) * 1000")) {
241  fprintf(stderr, "%s\n", mysql_error(writer));
242  exit(1);
243  }
244 
245  if (mysql_query(writer, "DELETE FROM cluster_usage_daily WHERE time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 3 YEAR)) * 1000")) {
246  fprintf(stderr, "%s\n", mysql_error(writer));
247  exit(1);
248  }
249 
250  if (mysql_query(writer, "DELETE FROM cluster_usage_monthly WHERE time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 5 YEAR)) * 1000")) {
251  fprintf(stderr, "%s\n", mysql_error(writer));
252  exit(1);
253  }
254 }
void Writer::Set_Database ( char *  db)

Sets name of database where you want to connect.

Parameters
[in]dbName of database

Definition at line 45 of file Writer.cpp.

46 {
47  database = db;
48 }
void Writer::Set_Off_Write_Day ( )

Does not calculate average load for the day and entry in the database

Definition at line 50 of file Writer.cpp.

51 {
52  write_day = false;
53 }
void Writer::Set_Off_Write_Month ( )

Does not calculate average load for the month and entry in the database

Definition at line 55 of file Writer.cpp.

56 {
57  write_month = false;
58 }
void Writer::Set_Password ( char *  psd)

Sets password of user that connects to database

Parameters
[in]psdPassword of user

Definition at line 40 of file Writer.cpp.

41 {
42  password = psd;
43 }
void Writer::Set_Server ( char *  serv)

Sets the server address where you want to connect.

Parameters
[in]servServer address

Definition at line 30 of file Writer.cpp.

31 {
32  server = serv;
33 }
void Writer::Set_User ( char *  usr)

Sets user name that connects to database

Parameters
[in]usrUser name

Definition at line 35 of file Writer.cpp.

36 {
37  user = usr;
38 }
void Writer::Write ( map< pair< string, string >, map< string, double > >  map_journal)

Produces log entry in the table 'cluster_usage_hourly' received from 'Keeper'

Parameters
[in]map_journalmap<pair<[type of node], [hostname]>, map<[name of parameter], [value]>>

Definition at line 136 of file Writer.cpp.

137 {
139 
140  for (map<pair<string, string>, map<string, double> >::iterator iter = map_journal.begin(); iter != map_journal.end(); iter++) {
141  string type = iter->first.first;
142  string nodename = iter->first.second;
143  double cpu_user = iter->second.find("cpu_user")->second;
144  double cpu_sys = iter->second.find("cpu_sys")->second;
145  double net_in = iter->second.find("net_in")->second;
146  double net_out = iter->second.find("net_out")->second;
147  double mem_total = iter->second.find("total")->second;
148  double mem_used = iter->second.find("mem_used")->second;
149  double mem_buffer = iter->second.find("buffer")->second;
150  double mem_cached = iter->second.find("cached")->second;
151  char* sql_query = zsys_sprintf("INSERT INTO cluster_usage_hourly (nodename, type, time, cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out) VALUES('%s', '%s', (UNIX_TIMESTAMP() * 1000), %f, %f, %f, %f, %f, %f, %f, %f)", nodename.data(), type.data(), cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out);
152  if (mysql_query(writer, sql_query)) {
153  fprintf(stderr, "%s\n", mysql_error(writer));
154  exit(1);
155  }
156  free(sql_query);
157  }
158 
159  if (write_day)
160  Write_Day();
161 }
void Write_Day()
Definition: Writer.cpp:163
void Check_Connection()
Definition: Writer.cpp:79
void Writer::Write_Day ( )

Calculates average load of the cluster during the day and writes in the table 'cluster_usage_daily'

Definition at line 163 of file Writer.cpp.

164 {
165  time(&t);
166  tk = localtime(&t);
167  if (cur_day != tk->tm_mday) {
168  if (mysql_query(reader, "SELECT type, nodename, AVG(cpu_user) as cpu_user, AVG(cpu_sys) AS cpu_sys, AVG(mem_total) AS mem_total, AVG(mem_used) AS mem_used, AVG(mem_buffer) AS mem_buffer, AVG(mem_cached) AS mem_cached, AVG(net_in) AS net_in, AVG(net_out) AS net_out FROM cluster_usage_hourly WHERE time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY)) * 1000 AND time < UNIX_TIMESTAMP(CURDATE()) * 1000 GROUP BY type, nodename ORDER BY time DESC")) {
169  fprintf(stderr, "%s\n", mysql_error(reader));
170  exit(1);
171  }
172 
173  res = mysql_use_result(reader);
174  while ((row = mysql_fetch_row(res)) != NULL) {
175  string type = row[0];
176  string nodename = row[1];
177  double cpu_user = atof(row[2]);
178  double cpu_sys = atof(row[3]);
179  double mem_total = atof(row[4]);
180  double mem_used = atof(row[5]);
181  double mem_buffer = atof(row[6]);
182  double mem_cached = atof(row[7]);
183  double net_in = atof(row[8]);
184  double net_out = atof(row[9]);
185 
186  char* sql_query = zsys_sprintf("INSERT INTO cluster_usage_daily (nodename, type, time, cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out) VALUES('%s', '%s', (UNIX_TIMESTAMP() * 1000), %f, %f, %f, %f, %f, %f, %f, %f)", nodename.data(), type.data(), cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out);
187  if (mysql_query(writer, sql_query)) {
188  fprintf(stderr, "%s\n", mysql_error(writer));
189  exit(1);
190  }
191  free(sql_query);
192  }
193  cur_day = tk->tm_mday;
194  mysql_free_result(res);
195  }
196 
197  if (write_month)
198  Write_Month();
199 }
void Write_Month()
Definition: Writer.cpp:201
void Writer::Write_Month ( )

Calculates average load of the cluster during the month and writes in the table 'cluster_usage_monthly'

Definition at line 201 of file Writer.cpp.

202 {
203  time(&t);
204  tk = localtime(&t);
205  if (cur_month != tk->tm_mon) {
206  if (mysql_query(reader, "SELECT type, nodename, AVG(cpu_user) as cpu_user, AVG(cpu_sys) AS cpu_sys, AVG(mem_total) AS mem_total, AVG(mem_used) AS mem_used, AVG(mem_buffer) AS mem_buffer, AVG(mem_cached) AS mem_cached, AVG(net_in) AS net_in, AVG(net_out) AS net_out FROM cluster_usage_daily WHERE time > UNIX_TIMESTAMP(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH))) * 1000 AND time < UNIX_TIMESTAMP(DATE_ADD(LAST_DAY(CURDATE() - INTERVAL 1 MONTH), INTERVAL 1 DAY)) * 1000 GROUP BY type, nodename ORDER BY time DESC")) {
207  fprintf(stderr, "%s\n", mysql_error(reader));
208  exit(1);
209  }
210 
211  res = mysql_use_result(reader);
212  while ((row = mysql_fetch_row(res)) != NULL) {
213  string type = row[0];
214  string nodename = row[1];
215  double cpu_user = atof(row[2]);
216  double cpu_sys = atof(row[3]);
217  double mem_total = atof(row[4]);
218  double mem_used = atof(row[5]);
219  double mem_buffer = atof(row[6]);
220  double mem_cached = atof(row[7]);
221  double net_in = atof(row[8]);
222  double net_out = atof(row[9]);
223 
224  char* sql_query = zsys_sprintf("INSERT INTO cluster_usage_monthly (nodename, type, time, cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out) VALUES('%s', '%s', (UNIX_TIMESTAMP() * 1000), %f, %f, %f, %f, %f, %f, %f, %f)", nodename.data(), type.data(), cpu_user, cpu_sys, mem_total, mem_used, mem_buffer, mem_cached, net_in, net_out);
225  if (mysql_query(writer, sql_query)) {
226  fprintf(stderr, "%s\n", mysql_error(writer));
227  exit(1);
228  }
229  free(sql_query);
230  }
231  cur_month = tk->tm_mon;
232  mysql_free_result(res);
233 
235  }
236 }
void Delete_Old_Records()
Definition: Writer.cpp:238

The documentation for this class was generated from the following files: