This сlass interacts with the database.
More...
#include <Writer.h>
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.
void Writer::Check_Connection |
( |
| ) |
|
Checks the connection. If it is broken, connect again.
Definition at line 79 of file Writer.cpp.
81 if(mysql_ping(writer) > 0)
83 if (!mysql_real_connect(writer, server, user, password, database, 0, NULL, 0)) {
84 fprintf(stderr,
"%s\n", mysql_error(writer));
88 if(mysql_ping(reader) > 0)
90 if (!mysql_real_connect(reader, server, user, password, database, 0, NULL, 0)) {
91 fprintf(stderr,
"%s\n", mysql_error(reader));
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.
118 if (mysql_query(writer,
"TRUNCATE TABLE cluster_usage_hourly")) {
119 fprintf(stderr,
"%s\n", mysql_error(writer));
123 if (mysql_query(writer,
"TRUNCATE TABLE cluster_usage_daily")) {
124 fprintf(stderr,
"%s\n", mysql_error(writer));
128 if (mysql_query(writer,
"TRUNCATE TABLE cluster_usage_monthly")) {
129 fprintf(stderr,
"%s\n", mysql_error(writer));
133 printf(
"Tables are cleared\n");
bool Writer::Connect_DB |
( |
| ) |
|
Establishes connection with database.
- Returns
- Returns success of connection
Definition at line 60 of file Writer.cpp.
62 writer = mysql_init(NULL);
63 reader = mysql_init(NULL);
65 if (!mysql_real_connect(writer, server, user, password, database, 0, NULL, 0)) {
66 fprintf(stderr,
"%s\n", mysql_error(writer));
70 if (!mysql_real_connect(reader, server, user, password, database, 0, NULL, 0)) {
71 fprintf(stderr,
"%s\n", mysql_error(reader));
75 printf(
"Сonnection to database is established\n");
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.
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));
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));
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));
113 printf(
"Tables are created\n");
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.
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));
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));
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));
void Writer::Set_Database |
( |
char * |
db | ) |
|
Sets name of database where you want to connect.
- Parameters
-
Definition at line 45 of file Writer.cpp.
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.
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.
void Writer::Set_Password |
( |
char * |
psd | ) |
|
Sets password of user that connects to database
- Parameters
-
Definition at line 40 of file Writer.cpp.
void Writer::Set_Server |
( |
char * |
serv | ) |
|
Sets the server address where you want to connect.
- Parameters
-
Definition at line 30 of file Writer.cpp.
void Writer::Set_User |
( |
char * |
usr | ) |
|
Sets user name that connects to database
- Parameters
-
Definition at line 35 of file Writer.cpp.
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_journal | map<pair<[type of node], [hostname]>, map<[name of parameter], [value]>> |
Definition at line 136 of file Writer.cpp.
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));
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.
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));
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]);
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));
193 cur_day = tk->tm_mday;
194 mysql_free_result(res);
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.
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));
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]);
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));
231 cur_month = tk->tm_mon;
232 mysql_free_result(res);
void Delete_Old_Records()
The documentation for this class was generated from the following files: