Skip to content

Commit 0d68065

Browse files
authored
Merge pull request #33 from kristrev/ft-restart-event
Export restart (system) events
2 parents 720bfc1 + b2b53a8 commit 0d68065

10 files changed

+283
-37
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
cmake_minimum_required(VERSION 2.6)
33
project(meta_exporter)
44

5-
set(CMAKE_C_FLAGS "-O1 -Wall -std=gnu99")
5+
set(CMAKE_C_FLAGS "-O1 -Wall -std=gnu99 -g")
66

77
set(LIBS pthread mnl)
88
set(SOURCE
@@ -40,6 +40,7 @@ if (SQLITE3)
4040
metadata_writer_json_helpers.c
4141
metadata_writer_inventory_conn.c
4242
metadata_writer_inventory_gps.c
43+
metadata_writer_inventory_system.c
4344
metadata_writer_sqlite_monitor.c)
4445
add_definitions("-DSQLITE_SUPPORT")
4546
endif()

files/metadata-exporter.conf.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"netlink": {
1717
"conn" : true,
1818
"pos" : true,
19-
"iface": true
19+
"iface": true,
20+
"system": true,
2021
},
2122
"sqlite": {
2223
"database": "/tmp/metadata.db",

metadata_exporter.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,38 @@ static struct json_object *create_fake_gps_rmc_obj()
223223
}
224224
#endif
225225

226+
static struct json_object *create_fake_restart_obj()
227+
{
228+
struct json_object *obj = NULL, *obj_add = NULL;
229+
struct timeval tv;
230+
231+
if (!(obj = json_object_new_object())) {
232+
return NULL;
233+
}
234+
235+
gettimeofday(&tv, NULL);
236+
if (!(obj_add = json_object_new_int64(tv.tv_sec))) {
237+
json_object_put(obj);
238+
return NULL;
239+
}
240+
json_object_object_add(obj, "timestamp", obj_add);
241+
242+
if (!(obj_add = json_object_new_int(META_TYPE_SYSTEM))) {
243+
json_object_put(obj);
244+
return NULL;
245+
}
246+
json_object_object_add(obj, "event_type", obj_add);
247+
248+
if (!(obj_add = json_object_new_string("861107030230685"))) {
249+
json_object_put(obj);
250+
return NULL;
251+
}
252+
json_object_object_add(obj, "imei", obj_add);
253+
254+
return obj;
255+
}
256+
257+
#if 0
226258
static struct json_object *create_fake_conn_obj(uint64_t l3_id, uint64_t l4_id,
227259
uint8_t event_param, char *event_value_str, uint64_t tstamp)
228260
{
@@ -373,6 +405,7 @@ static struct json_object *create_fake_conn_obj(uint64_t l3_id, uint64_t l4_id,
373405

374406
return obj;
375407
}
408+
#endif
376409

377410
static ssize_t send_netlink_json(uint8_t *snd_buf,
378411
struct json_object *parsed_obj, int32_t sock_fd,
@@ -552,16 +585,18 @@ static void test_netlink(uint32_t packets)
552585
while(1) {
553586
gettimeofday(&tv, NULL);
554587

555-
if (i == 0)
588+
/*if (i == 0)
556589
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,", i+1);
557590
else
558-
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,4", i+1);
591+
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,4", i+1);*/
559592

560593
/*if (i < 4)
561594
obj_to_send = create_fake_conn_obj(1, 2, CONN_EVENT_L3_UP, "1,2,1", i+1);
562595
else
563596
obj_to_send = create_fake_conn_obj(1, 2, CONN_EVENT_DATA_USAGE_UPDATE, "1,2,1,4", tv.tv_sec);*/
564597

598+
obj_to_send = create_fake_restart_obj();
599+
565600
if (!obj_to_send)
566601
continue;
567602

metadata_exporter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#define META_TYPE_MUNIN 0x05
5454
#define META_TYPE_SYSEVENT 0x06
5555
#define META_TYPE_RADIO 0x08
56+
#define META_TYPE_SYSTEM 0x10
5657

5758
enum iface_event {
5859
IFACE_EVENT_DEV_STATE=1,
@@ -190,6 +191,8 @@ struct md_iface_event {
190191
uint8_t event_type;
191192
};
192193

194+
typedef struct md_iface_event md_system_event_t;
195+
193196
struct md_conn_event {
194197
MD_EVENT;
195198
uint8_t event_type;

metadata_input_netlink.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,19 @@ static uint8_t md_input_netlink_add_json_key_value(const char *key,
679679
return RETVAL_SUCCESS;
680680
}
681681

682+
static void md_input_netlink_handle_system_event(struct md_input_netlink *min,
683+
struct json_object *obj)
684+
{
685+
//recycle iface event, it contains all fields we need (currently)
686+
memset(min->mse, 0, sizeof(md_system_event_t));
687+
min->mse->md_type = META_TYPE_SYSTEM;
688+
689+
if (md_input_netlink_parse_iface_event(min, obj, min->mse) == RETVAL_FAILURE)
690+
return;
691+
692+
mde_publish_event_obj(min->parent, (struct md_event*) min->mse);
693+
}
694+
682695
static void md_input_netlink_handle_event(void *ptr, int32_t fd, uint32_t events)
683696
{
684697
struct md_input_netlink *min = ptr;
@@ -746,6 +759,9 @@ static void md_input_netlink_handle_event(void *ptr, int32_t fd, uint32_t events
746759
case META_TYPE_RADIO:
747760
md_input_netlink_handle_radio_event(min, nlh_obj);
748761
break;
762+
case META_TYPE_SYSTEM:
763+
md_input_netlink_handle_system_event(min, nlh_obj);
764+
break;
749765
default:
750766
META_PRINT(min->parent->logfile, "Unknown event type\n");
751767
break;
@@ -773,7 +789,7 @@ static uint8_t md_input_netlink_config(struct md_input_netlink *min)
773789
backend_event_loop_update(min->parent->event_loop, EPOLLIN, EPOLL_CTL_ADD,
774790
mnl_socket_get_fd(min->metadata_sock), min->event_handle);
775791

776-
//TODO: Move to handler
792+
//TODO: guard with check for flag
777793
min->mce = calloc(sizeof(struct md_conn_event), 1);
778794
if (min->mce == NULL)
779795
return RETVAL_FAILURE;
@@ -786,6 +802,11 @@ static uint8_t md_input_netlink_config(struct md_input_netlink *min)
786802
if (min->mre == NULL)
787803
return RETVAL_FAILURE;
788804

805+
min->mse = calloc(sizeof(md_system_event_t), 1);
806+
if (min->mre == NULL)
807+
return RETVAL_FAILURE;
808+
809+
789810
return RETVAL_SUCCESS;
790811
}
791812

@@ -797,14 +818,17 @@ static uint8_t md_input_netlink_init(void *ptr, json_object* config)
797818
json_object* subconfig;
798819
if (json_object_object_get_ex(config, "netlink", &subconfig)) {
799820
json_object_object_foreach(subconfig, key, val) {
800-
if (!strcmp(key, "conn"))
821+
if (!strcmp(key, "conn")) {
801822
md_nl_mask |= META_TYPE_CONNECTION;
802-
else if (!strcmp(key, "pos"))
823+
} else if (!strcmp(key, "pos")) {
803824
md_nl_mask |= META_TYPE_POS;
804-
else if (!strcmp(key, "iface"))
825+
} else if (!strcmp(key, "iface")) {
805826
md_nl_mask |= META_TYPE_INTERFACE;
806-
else if (!strcmp(key, "radio"))
827+
} else if (!strcmp(key, "radio")) {
807828
md_nl_mask |= META_TYPE_RADIO;
829+
} else if (!strcmp(key, "system")) {
830+
md_nl_mask |= META_TYPE_SYSTEM;
831+
}
808832
}
809833
}
810834

@@ -826,6 +850,7 @@ void md_netlink_usage()
826850
fprintf(stderr, " \"pos\":\t\tReceive netlink position events\n");
827851
fprintf(stderr, " \"iface\":\t\tReceive netlink interface events\n");
828852
fprintf(stderr, " \"radio\":\t\tReceive netlink radio events (QXDM + neigh. cells)\n");
853+
fprintf(stderr, " \"system\":\t\tReceive netlink system (reboot) events\n");
829854
fprintf(stderr, "},\n");
830855
}
831856

metadata_input_netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct md_input_netlink {
3838
struct md_conn_event *mce;
3939
struct md_iface_event *mie;
4040
struct md_radio_event *mre;
41+
md_system_event_t *mse;
4142
};
4243

4344
void md_netlink_usage();

metadata_writer_inventory_system.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <string.h>
2+
#include <stdint.h>
3+
4+
#include "metadata_writer_inventory_system.h"
5+
#include "metadata_exporter_log.h"
6+
#include "metadata_writer_sqlite_helpers.h"
7+
#include "metadata_writer_json_helpers.h"
8+
9+
uint8_t md_inventory_handle_system_event(struct md_writer_sqlite *mws,
10+
md_system_event_t *mse)
11+
{
12+
sqlite3_stmt *stmt = mws->insert_system;
13+
sqlite3_clear_bindings(stmt);
14+
sqlite3_reset(stmt);
15+
16+
if (sqlite3_bind_int(stmt, 1, mws->node_id) ||
17+
sqlite3_bind_int(stmt, 2, mws->session_id) || // BootCount
18+
sqlite3_bind_int(stmt, 3, mws->session_id_multip) || // BootMultiplier
19+
sqlite3_bind_int(stmt, 4, mse->tstamp) ||
20+
sqlite3_bind_int(stmt, 5, mse->sequence) ||
21+
sqlite3_bind_text(stmt, 6, mse->imei, strlen(mse->imei),
22+
SQLITE_STATIC)) {
23+
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to bind values to "
24+
"INSERT query (system)\n");
25+
return RETVAL_FAILURE;
26+
}
27+
28+
if (sqlite3_step(stmt) != SQLITE_DONE) {
29+
return RETVAL_FAILURE;
30+
} else {
31+
return RETVAL_SUCCESS;
32+
}
33+
}
34+
35+
static uint8_t md_inventory_system_dump_db_json(struct md_writer_sqlite *mws,
36+
FILE *output)
37+
{
38+
const char *json_str;
39+
sqlite3_reset(mws->dump_system);
40+
41+
json_object *jarray = json_object_new_array();
42+
43+
if (md_json_helpers_dump_write(mws->dump_system, jarray))
44+
{
45+
json_object_put(jarray);
46+
return RETVAL_FAILURE;
47+
}
48+
49+
json_str = json_object_to_json_string_ext(jarray, JSON_C_TO_STRING_PLAIN);
50+
fprintf(output, "%s", json_str);
51+
52+
json_object_put(jarray);
53+
return RETVAL_SUCCESS;
54+
}
55+
56+
static uint8_t md_inventory_system_delete_db(struct md_writer_sqlite *mws)
57+
{
58+
int32_t retval;
59+
60+
sqlite3_reset(mws->delete_system);
61+
retval = sqlite3_step(mws->delete_system);
62+
63+
if (retval != SQLITE_DONE) {
64+
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to delete system %s\n",
65+
sqlite3_errstr(retval));
66+
return RETVAL_FAILURE;
67+
}
68+
69+
return RETVAL_SUCCESS;
70+
}
71+
72+
uint8_t md_inventory_system_copy_db(struct md_writer_sqlite *mws)
73+
{
74+
uint8_t retval = RETVAL_SUCCESS;
75+
76+
retval = md_writer_helpers_copy_db(mws->system_prefix,
77+
mws->system_prefix_len, md_inventory_system_dump_db_json, mws,
78+
md_inventory_system_delete_db);
79+
80+
if (retval == RETVAL_SUCCESS)
81+
mws->num_system_events = 0;
82+
83+
return retval;
84+
}

metadata_writer_inventory_system.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef METADATA_WRITER_INVENTORY_SYSTEM_H
2+
#define METADATA_WRITER_INVENTORY_SYSTEM_H
3+
4+
#include "metadata_exporter.h"
5+
#include "metadata_writer_sqlite.h"
6+
7+
uint8_t md_inventory_handle_system_event(struct md_writer_sqlite *mws,
8+
md_system_event_t *mse);
9+
uint8_t md_inventory_system_copy_db(struct md_writer_sqlite *mws);
10+
11+
#endif

0 commit comments

Comments
 (0)