int main(int argc, char* argv[]) { int debugging = 0, broken_only = 0; #ifdef WITH_DB42 int id_only = 0; #endif char *output_file = NULL; FILE *fp; int rv; DBC *cur; char *dn; CacheEntry entry; univention_debug_init("stderr", 1, 1); /* parse arguments */ for (;;) { int c; #ifdef WITH_DB42 c = getopt(argc, argv, "d:c:O:ri"); #else c = getopt(argc, argv, "d:c:O:r"); #endif if (c < 0) break; switch (c) { case 'd': debugging=atoi(optarg); break; case 'c': cache_dir=strdup(optarg); break; case 'O': if (strcmp(optarg, "-") != 0) output_file=strdup(optarg); break; case 'r': broken_only=1; break; #ifdef WITH_DB42 case 'i': id_only=1; break; #endif default: usage(); exit(1); } } if (debugging > 1) { univention_debug_set_level(UV_DEBUG_LISTENER, UV_DEBUG_ALL); univention_debug_set_level(UV_DEBUG_LDAP, UV_DEBUG_ALL); univention_debug_set_level(UV_DEBUG_KERBEROS, UV_DEBUG_ALL); } else if ( debugging > 0 ) { univention_debug_set_level(UV_DEBUG_LISTENER, UV_DEBUG_INFO); univention_debug_set_level(UV_DEBUG_LDAP, UV_DEBUG_INFO); univention_debug_set_level(UV_DEBUG_KERBEROS, UV_DEBUG_INFO); } else { univention_debug_set_level(UV_DEBUG_LISTENER, UV_DEBUG_ERROR); univention_debug_set_level(UV_DEBUG_LDAP, UV_DEBUG_ERROR); univention_debug_set_level(UV_DEBUG_KERBEROS, UV_DEBUG_ERROR); } if (output_file) { fp = fopen(output_file, "w"); } else { fp = stdout; } if (fp == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "Couldn't open dump file"); exit(1); } if (cache_init() != 0) exit(1); #ifdef WITH_DB42 if (id_only) { CacheMasterEntry master_entry; cache_get_master_entry(&master_entry); printf("%ld %ld\n", master_entry.id, master_entry.schema_id); } else { exit(0); #endif for (rv=cache_first_entry(&cur, &dn, &entry); rv != DB_NOTFOUND; rv=cache_next_entry(&cur, &dn, &entry)) { if ((rv == 0 && !broken_only) || (rv == -1 && broken_only)) { cache_dump_entry(dn, &entry, fp); cache_free_entry(&dn, &entry); fprintf(fp, "\n"); } if (rv < -1) break; } cache_free_cursor(cur); #ifdef WITH_DB42 } #endif cache_close(); return 0; }
static void jsonout_export(void) { unsigned int entries = 0; unsigned int exported = 0; struct spblk *x, *nextx; struct sptree *sp; struct cache_ent *e; char *json = NULL; int got_pos; char tbuf[TBUF_LEN]; time_t now; time(&now); time_jsonais(&now, tbuf, TBUF_LEN); /* fill in initial json */ json = str_append(json, "{\n" "\t\"protocol\": \"jsonais\",\n" "\t\"encodetime\": \"%s\",\n" "\t\"groups\": [\n" /* start of groups */ "\t\t{\n" /* start of group */ "\t\t\t\"path\": [ { \"name\": \"%s\" } ],\n" "\t\t\t\"msgs\": [\n", tbuf, mycall ); /* get the current position cache */ sp = cache_rotate(); /* expire old entries */ for (x = sp_fhead(sp); x != NULL; x = nextx) { entries++; nextx = sp_fnext(x); e = (struct cache_ent *)x->data; got_pos = ((e->lat > 0.0001 || e->lat < -0.0001) && (e->lon > 0.0001 || e->lon < -0.0001)); if ((e->mmsi) && (got_pos) ) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d position", e->mmsi); time_jsonais(&e->received_pos, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 3, \"mmsi\": %d, \"rxtime\": \"%s\"", (exported == 0) ? "" : ",\n", e->mmsi, tbuf ); json = str_append(json, ", \"lat\": %.7f, \"lon\": %.7f", e->lat, e->lon ); if (e->course >= 0) json = str_append(json, ", \"course\": %.1f", e->course); if (e->hdg >= 0) json = str_append(json, ", \"heading\": %d", e->hdg); if (e->sog >= 0) json = str_append(json, ", \"speed\": %.1f", e->sog); if (e->navstat >= 0) json = str_append(json, ", \"status\": %d", e->navstat); json = str_append(json, "}"); exported++; } if ((e->mmsi) && (e->name) ) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d data", e->mmsi); time_jsonais(&e->received_data, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 5, \"mmsi\": %d, \"rxtime\": \"%s\"", (exported == 0) ? "" : ",\n", e->mmsi, tbuf ); if (e->imo >= 0) json = str_append(json, ", \"imo\": %d", e->imo); if (e->shiptype >= 0) json = str_append(json, ", \"shiptype\": %d", e->shiptype); if (e->callsign) json = str_append(json, ", \"callsign\": \"%s\"", e->callsign); if (e->name) json = str_append(json, ", \"shipname\": \"%s\"", e->name); if (e->destination) json = str_append(json, ", \"destination\": \"%s\"", e->destination); if (e->A >= 0 && e->B >= 0) { json = str_append(json, ", \"length\": %d", e->A + e->B); json = str_append(json, ", \"ref_front\": %d", e->A); } if (e->draught >= 0) json = str_append(json, ", \"draught\": %.1f", e->draught); if (e->C >= 0 && e->D >= 0) { json = str_append(json, ", \"width\": %d", e->C + e->D); json = str_append(json, ", \"ref_left\": %d", e->C); } json = str_append(json, "}"); exported++; } if (e->persons_on_board >= 0) { hlog(LOG_DEBUG, "jsonout: exporting MMSI %d persons_on_board %d", e->mmsi, e->persons_on_board); time_jsonais(&e->received_persons_on_board, tbuf, TBUF_LEN); json = str_append(json, "%s{\"msgtype\": 8, \"mmsi\": %d, \"persons_on_board\": %d, \"rxtime\": \"%s\"}", (exported == 0) ? "" : ",\n", e->mmsi, e->persons_on_board, tbuf ); exported++; } cache_free_entry(e); sp_delete(x, sp); } json = str_append(json, "\n\n" "\t\t\t]\n" /* end of message array */ "\t\t}\n" /* end of the message group */ "\t]\n" /* end of groups */ "}\n" /* end of the whole json blob */ ); /* clean up */ if (sp) { sp_null(sp); hfree(sp); } hlog(LOG_DEBUG, "jsonout: %s", json); if (exported) { /* if we have some entries, send them out */ jsonout_post_all(json); } hfree(json); }