395 |
395 |
396 |
396 |
397 int bms_mysql_query(const char *query) |
397 int bms_mysql_query(const char *query) |
398 { |
398 { |
399 int rc = mysql_query(con, query); |
399 int rc = mysql_query(con, query); |
|
400 int err = mysql_errno(con); |
400 |
401 |
401 if (rc) { |
402 if (rc) { |
402 syslog(LOG_NOTICE, "MySQL: error %u (%s)", mysql_errno(con), mysql_error(con)); |
403 syslog(LOG_NOTICE, "MySQL: error %u (%s)", err, mysql_error(con)); |
403 syslog(LOG_NOTICE, query); |
404 syslog(LOG_NOTICE, query); |
404 } else { |
405 } else { |
405 return 0; |
406 return 0; |
406 } |
407 } |
407 |
408 |
408 /* Any error execpt server gone away */ |
409 /* Any error execpt server gone away */ |
409 if (rc != 2006) |
410 if (err != 2006) |
410 return rc; |
411 return rc; |
411 |
412 |
|
413 syslog(LOG_NOTICE, "Trying to reconnect"); |
412 /* Try to reconnect and do the query again */ |
414 /* Try to reconnect and do the query again */ |
413 mysql_close(con); |
415 mysql_close(con); |
414 if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
416 if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
415 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); |
417 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); |
416 return 2; |
418 return 2; |
432 |
434 |
433 |
435 |
434 |
436 |
435 void node_mysql_insert(sys_node_list *node) |
437 void node_mysql_insert(sys_node_list *node) |
436 { |
438 { |
437 char *query = malloc(1024), first[21], last[21]; |
439 char *query = malloc(1024), first[73], last[73]; |
438 struct tm *mytime; |
440 struct tm *mytime; |
439 |
441 |
440 mytime = localtime(&node->firstseen); |
442 mytime = localtime(&node->firstseen); |
441 snprintf(first, 20, "%04d-%02d-%02d %02d:%02d:%02d", |
443 snprintf(first, 72, "%04d-%02d-%02d %02d:%02d:%02d", |
442 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
444 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
443 mytime = localtime(&node->lastseen); |
445 mytime = localtime(&node->lastseen); |
444 snprintf(last, 20, "%04d-%02d-%02d %02d:%02d:%02d", |
446 snprintf(last, 72, "%04d-%02d-%02d %02d:%02d:%02d", |
445 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
447 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
446 |
448 |
447 snprintf(query, 1023, |
449 snprintf(query, 1023, |
448 "INSERT INTO mon_nodes SET uuid='%s', node='%s', online='%s', group_id='%s', " \ |
450 "INSERT INTO mon_nodes SET uuid='%s', node='%s', online='%s', group_id='%s', " \ |
449 "hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', firstseen='%s', lastseen='%s', " \ |
451 "hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', firstseen='%s', lastseen='%s', " \ |
463 |
465 |
464 |
466 |
465 |
467 |
466 void node_mysql_update(sys_node_list *node) |
468 void node_mysql_update(sys_node_list *node) |
467 { |
469 { |
468 char *query = malloc(1024), last[21]; |
470 char *query = malloc(1024), last[65]; |
469 struct tm *mytime; |
471 struct tm *mytime; |
470 |
472 |
471 mytime = localtime(&node->lastseen); |
473 mytime = localtime(&node->lastseen); |
472 snprintf(last, 20, "%04d-%02d-%02d %02d:%02d:%02d", |
474 snprintf(last, 64, "%04d-%02d-%02d %02d:%02d:%02d", |
473 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
475 mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min, mytime->tm_sec); |
474 |
476 |
475 snprintf(query, 1023, |
477 snprintf(query, 1023, |
476 "UPDATE mon_nodes SET online='%s', hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', lastseen='%s', " \ |
478 "UPDATE mon_nodes SET online='%s', hardwaremake='%s', hardwaremodel='%s', os='%s', os_version='%s', firmware='%s', lastseen='%s', " \ |
477 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \ |
479 "temperature='%.3f', humidity='%.3f', barometer='%.3f', gps_latitude='%.8f', gps_longitude='%.8f', gps_altitude='%.8f', " \ |