int read_cache_dir(const char* cache_dir, const char* rrd_dir, const char* timestamp) { // walk through all files in cache_dir (do not recurse ...) // and add the files to the rras that should be stored in rra-dir // create the rrd files if they do not exist DIR* dir = opendir(cache_dir); if (!dir) { fprintf(stderr, "ERROR: Could not open cache-dir \"%s\": %s\n", cache_dir, strerror(errno)); exit(-4); } struct dirent* d; size_t counter = 0; while (NULL != (d = readdir(dir))) { if (d->d_type & DT_REG || d->d_type == DT_UNKNOWN) { // we are only interested in the file snmp_info_dumps.txt if (strcmp(d->d_name, "snmp_info_dumps.txt")) { // not our file continue; } // we found our file. parse it and quit char cache_filename[1024]; snprintf(cache_filename, 1024, "%s/%s", cache_dir, d->d_name); read_cache_file(cache_filename, rrd_dir, timestamp); closedir(dir); return 0; } } fprintf(stderr, "ERROR: Could not find a SNMP file in directory!\n"); closedir(dir); return 0; }
int main(int argc, char** argv) { if (argc != 4) { fprintf(stderr, "Usage: %s <cache-file> <rra-dir> <timestamp>\n", argv[0]); return -1; } char* cache_file = argv[1]; char* rra_dir = argv[2]; char* timestamp = argv[3]; // check if cache_file is a file and check if we can read from it struct stat s; if (-1 == stat(cache_file, &s)) { fprintf(stderr, "ERROR stating cache-file \"%s\": %s\n", cache_file, strerror(errno)); exit(-1); } /* if (!(s.st_mode & S_IFDIR)) { fprintf(stderr, "ERROR: cache-dir is not a directory!\n"); exit(-2); } if (-1 == access(cache_dir, R_OK | X_OK)) { fprintf(stderr, "ERROR: Insufficient rights on cache-dir \"%s\". Need read and execute rights!\n", cache_dir); exit(-3); } */ // check if the rra_dir is a directory and if we have write access to it if (-1 == stat(rra_dir, &s)) { fprintf(stderr, "ERROR stating rra-dir \"%s\": %s\n", rra_dir, strerror(errno)); exit(-1); } if (-1 == access(rra_dir, R_OK | X_OK | W_OK)) { fprintf(stderr, "ERROR: Insufficient rights on rra-dir \"%s\". Need read, write, and execute rights!\n", rra_dir); exit(-3); } char tsdb_file[2*LINE_SIZE]; int ret; snprintf(tsdb_file, 2*LINE_SIZE, "%s/%s", rra_dir, "snmp.tsdb"); init_tsdb(tsdb_file); int t = atoi(timestamp); ret = tsdb_goto_epoch(&db, t, 0, 1); if (ret != 0) { fprintf(stderr, "RROR: tsdb failed to go to epoch %lu\n", t); exit(1); } read_cache_file(cache_file, rra_dir, timestamp); tsdb_flush(&db); close_tsdb(); return 0; }
void cache_object_init(struct cache_object *co) { cache_index_init(&co->ci); cache_init(&co->cc); open_index_file(&co->ci); read_cache_index_file(&co->ci); open_cache_file(&co->cc); read_cache_file(&co->cc); }
/* * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>] * * The cache-file can be quite important for big trees. This is an * expensive operation if you have to walk the whole chain of * parents in a tree with a long revision history. */ int main(int argc, char **argv) { int i; int nr = 0; unsigned char sha1[MAX_COMMITS][20]; /* * First - pick up all the revisions we can (both from * caches and from commit file chains). */ for (i = 1; i < argc ; i++) { char *arg = argv[i]; if (!strcmp(arg, "--cache")) { read_cache_file(argv[2]); i++; continue; } if (!strcmp(arg, "--edges")) { show_edges = 1; continue; } if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr])) usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]"); parse_commit(sha1[nr]); nr++; } /* * Now we have the maximal tree. Walk the different sha files back to the root. */ for (i = 0; i < nr; i++) mark_sha1_path(lookup_rev(sha1[i]), 1 << i); /* * Now print out the results.. */ for (i = 0; i < nr_revs; i++) { struct revision *rev = revs[i]; struct parent *p; if (!interesting(rev)) continue; printf("%s:%d", sha1_to_hex(rev->sha1), marked(rev)); p = rev->parent; while (p) { printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent)); p = p->next; } printf("\n"); } return 0; }
IPDB * init_db(char * cities_csv_file, char * ranges_csv_file, char * cache_file_name){ if(DEBUG) printf("Initializing db"); IPDB *db; db = (IPDB*)malloc(sizeof(IPDB)); if (db == NULL) //no memory left return NULL; db->cities = NULL; db->ranges = NULL; db->cache_file_name = cache_file_name; db->cities_csv_file = cities_csv_file; db->max_cities_count = MAX_CITIES_COUNT; db->ranges_csv_file = ranges_csv_file; db->max_ranges_count = MAX_RANGES_COUNT; db->isps_count = 0; if(USE_CACHE && read_cache_file(db) > 0){ if(DEBUG) printf("Loaded DB from Cache-File with %i records \n", db->ranges_count); }else{ if(DEBUG) printf("Initializing IPDB from CSV-file: %s \n", db->ranges_csv_file); read_cities_csv(db); if(db->cities_count == 0) { return NULL; } sort_cities(db); read_ranges_csv(db); if(db!=NULL && db->ranges_count > 0 && USE_CACHE) { if(DEBUG) printf("Got %i records from CSV-file, writing to cache...\n", db->ranges_count); write_cache_file(db); } } return db; }
int main(int argc, char *argv[]) { struct cmdline_options opts = { .cache_file = CACHE_FILE, .base_oid = BASE_OID, .conf_file = CONF_FILE, .cache_prog = CACHE_REGEN_PROG, }; bool is_ok; int cache_fd; struct cache_entry *cache_entries; size_t num_cache_entries; int rc; unsigned int attr_idx = SNMPD_IDX_UNKNOWN; size_t req_cache_idx; struct oid_info const *sub_oid; size_t base_oid_len; while (1) { int c = getopt_long(argc, argv, "gns", CMDLINE_OPTIONS, 0); if (c==-1) break; switch (c) { case CMD_HELP: show_help(); case CMD_VERSION: show_version(); case CMD_BASE_OID: opts.base_oid = optarg; break; case CMD_CACHE: opts.cache_file = optarg; break; case CMD_CACHE_PROGRAM: opts.cache_prog = optarg; break; case CMD_CONF: opts.conf_file = optarg; break; case 'g': opts.op_get = 1; break; case 's': opts.op_set = 1; break; case 'n': opts.op_get_next = 1; break; default: fputs("Try '--help' for more information.\n", stderr); exit(EX_USAGE); } } is_ok = false; base_oid_len = strlen(opts.base_oid); if (opts.op_get + opts.op_set + opts.op_get_next > 1) fputs("more than one operation specified\n", stderr); else if (opts.op_get + opts.op_set + opts.op_get_next == 0) fputs("no operation specified\n", stderr); else if (optind + 1 != argc) fputs("no/too much OID specified\n", stderr); else if (strncmp(opts.base_oid, argv[optind], base_oid_len) && argv[optind][base_oid_len] != '\0' && argv[optind][base_oid_len] != '.') fputs("unsupported OID\n", stderr); else if (!parse_oid(&argv[optind][base_oid_len], &sub_oid, &attr_idx)) ; /* noop */ else if ((opts.op_get || opts.op_set) && (sub_oid == NULL || attr_idx == SNMPD_IDX_UNKNOWN || (sub_oid->num_suboid && attr_idx >= sub_oid->num_suboid))) { fprintf(stderr, "sub_oid=%p, idx=%u\n", sub_oid, attr_idx); fputs("unknown OID\n", stderr); return EX_UNAVAILABLE; } else is_ok = true; if (!is_ok) exit(EX_USAGE); if (opts.op_set) { puts("not-writable"); return EXIT_SUCCESS; } if (opts.op_get_next && sub_oid == NULL) { sub_oid = &SUB_OIDS[0]; assert(attr_idx == SNMPD_IDX_UNKNOWN); } assert(sub_oid != NULL); cache_fd = open_cache_file(opts.conf_file, opts.cache_file, opts.cache_prog); if (cache_fd < 0) exit(-cache_fd); rc = read_cache_file(cache_fd, &cache_entries, &num_cache_entries); if (rc < 0) exit(-rc); close(cache_fd); if (sub_oid->num_suboid == 0) { size_t i; if (attr_idx == SNMPD_IDX_UNKNOWN && opts.op_get_next) req_cache_idx = 0; else req_cache_idx = num_cache_entries; for (i = 0; i < num_cache_entries; ++i) { if (cache_entries[i].idx != attr_idx) continue; req_cache_idx = i; if (opts.op_get_next) { ++req_cache_idx; if (req_cache_idx >= num_cache_entries) { req_cache_idx = 0; ++sub_oid; } } break; } } else if (opts.op_get_next) { if (attr_idx == SNMPD_IDX_UNKNOWN) req_cache_idx = 0; else if (attr_idx + 1 != sub_oid->num_suboid) req_cache_idx = attr_idx; else { req_cache_idx = 0; ++sub_oid; } } else req_cache_idx = attr_idx; assert(sub_oid->num_suboid == 0 || req_cache_idx < sub_oid->num_suboid); if (sub_oid->idx == SNMPD_OID_METADATA) { print_metadata(opts.base_oid, sub_oid, req_cache_idx, num_cache_entries); } else if (req_cache_idx < num_cache_entries && sub_oid->oid != 0) { rc = read_sysfs_cache(&cache_entries[req_cache_idx]); if (rc < 0) exit(-rc); print_cache(&cache_entries[req_cache_idx], opts.base_oid, sub_oid); } free(cache_entries); }