int do_catalog_query( time_t stoptime ) { int i = 0; //global_catalog iterator if(resource_mode == 0 && format_mode == FORMAT_TABLE) nvpair_print_table_header(stdout, queue_headers); else if(resource_mode) nvpair_print_table_header(stdout, master_resource_headers); while(global_catalog[i] != NULL){ if(!(resource_mode || format_mode == FORMAT_TABLE)){ //long options nvpair_print_text(global_catalog[i], stdout); }else{ const char *temp_my_master = nvpair_lookup_string(global_catalog[i], "my_master"); if( !temp_my_master || !strcmp(temp_my_master, "127.0.0.1:-1") ) { //this master has no master if(resource_mode) { debug(D_WQ,"%s resources -- cores:%s memory:%s disk:%s\n",nvpair_lookup_string(global_catalog[i],"project"),nvpair_lookup_string(global_catalog[i],"cores_total"),nvpair_lookup_string(global_catalog[i],"memory_total"),nvpair_lookup_string(global_catalog[i],"disk_total")); nvpair_print_table(global_catalog[i], stdout, master_resource_headers); }else if(format_mode == FORMAT_TABLE){ nvpair_print_table(global_catalog[i], stdout, queue_headers); } int space = 1; my_foreman(&space, nvpair_lookup_string(global_catalog[i], "name"), atoi(nvpair_lookup_string(global_catalog[i], "port")), stoptime); } } i++; } if(format_mode == FORMAT_TABLE){ nvpair_print_table_footer(stdout, queue_headers); }else if(resource_mode){ nvpair_print_table_footer(stdout, master_resource_headers); } global_catalog_cleanup(); return EXIT_SUCCESS; }
static void log_create( struct nvpair_database *db, const char *key, struct nvpair *nv ) { log_select(db); log_time(db); fprintf(db->logfile,"C %s\n",key); nvpair_print_text(nv,db->logfile); }
static int checkpoint_read( struct deltadb *db ) { FILE * file = stdin; if(!file) return 0; char firstline[NVPAIR_LINE_MAX]; fgets(firstline, sizeof(firstline), file); printf("%s",firstline); while(1) { int keep = 0; struct nvpair *nv = nvpair_create(); int num_pairs = nvpair_parse_stream(nv,file); if(num_pairs>0) { const char *key = nvpair_lookup_string(nv,"key"); if(key) { keep = 1; struct argument *arg = db->args; while (arg!=NULL){ const char *var = nvpair_lookup_string(nv,arg->param); if ( var!=NULL && keep_object(arg,var)==0 ){ keep = 0; break; } arg = arg->next; } nvpair_delete(hash_table_remove(db->table,key)); if (keep==1){ nvpair_print_text(nv,stdout); hash_table_insert(db->table,key,nv); } } else debug(D_NOTICE,"no key in object create."); } else if (num_pairs == -1) { return 1; } else { break; } if (!keep) nvpair_delete(nv); } return 1; }
int do_direct_query( const char *master_host, int master_port, time_t stoptime ) { static struct nvpair_header *query_headers[4] = { queue_headers, task_headers, worker_headers, master_resource_headers }; static const char * query_strings[4] = {"queue","task","worker", "master_resource"}; struct nvpair_header *query_header = query_headers[query_mode]; const char * query_string = query_strings[query_mode]; struct link *l; struct nvpair *nv; char master_addr[LINK_ADDRESS_MAX]; if(!domain_name_cache_lookup(master_host,master_addr)) { fprintf(stderr,"couldn't find address of %s\n",master_host); return 1; } l = link_connect(master_addr,master_port,stoptime); if(!l) { fprintf(stderr,"couldn't connect to %s port %d: %s\n",master_host,master_port,strerror(errno)); return 1; } link_putfstring(l,"%s_status\n",stoptime,query_string); if(format_mode==FORMAT_TABLE) { nvpair_print_table_header(stdout, query_header); } while((nv = link_nvpair_read(l,stoptime))) { if(format_mode == FORMAT_TABLE) { nvpair_print_table(nv, stdout, query_header); } else { nvpair_print_text(nv, stdout); } nvpair_delete(nv); } if(format_mode == FORMAT_TABLE) { nvpair_print_table_footer(stdout, query_header); } return EXIT_SUCCESS; }
static int checkpoint_write( struct nvpair_database *db, const char *filename ) { char *key; struct nvpair *nv; FILE *file = fopen(filename,"w"); if(!file) return 0; hash_table_firstkey(db->table); while((hash_table_nextkey(db->table,&key,(void**)&nv))) { fprintf(file,"key %s\n",key); nvpair_print_text(nv,file); } fclose(file); return 1; }
int main(int argc, char *argv[]) { struct catalog_query *cq; struct nvpair *nv; work_queue_status_parse_command_line_arguments(argc, argv); if(optind > argc) { work_queue_status_show_help("work_queue_status"); exit(EXIT_FAILURE); } cq = catalog_query_create(CATALOG_HOST, CATALOG_PORT, time(0) + Work_Queue_Status_Timeout); if(!cq) { fprintf(stderr, "couldn't query catalog %s:%d: %s\n", CATALOG_HOST, CATALOG_PORT, strerror(errno)); return 1; } if(Work_Queue_Status_Mode == MODE_TABLE) nvpair_print_table_header(stdout, headers); while((nv = catalog_query_read(cq, time(0) + Work_Queue_Status_Timeout))) { if(strcmp(nvpair_lookup_string(nv, "type"), CATALOG_TYPE_WORK_QUEUE_MASTER) == 0) { if(Work_Queue_Status_Mode == MODE_TABLE) nvpair_print_table(nv, stdout, headers); else nvpair_print_text(nv, stdout); } nvpair_delete(nv); } if(Work_Queue_Status_Mode == MODE_TABLE) nvpair_print_table_footer(stdout, headers); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { struct catalog_query *q; struct nvpair *n; time_t timeout = 60, stoptime; const char *catalog_host = 0; signed char c; int i; int count = 0; int mode = MODE_TABLE; INT64_T total = 0, avail = 0; const char *filter_name = 0; const char *filter_value = 0; debug_config(argv[0]); static struct option long_options[] = { {"catalog", required_argument, 0, 'c'}, {"debug", required_argument, 0, 'd'}, {"debug-file", required_argument, 0, 'o'}, {"debug-rotate-max", required_argument, 0, 'O'}, {"server-space", required_argument, 0, 'A'}, {"all", no_argument, 0, 'a'}, {"timeout", required_argument, 0, 't'}, {"brief", no_argument, 0, 's'}, {"verbose", no_argument, 0, 'l'}, {"totals", no_argument, 0, 'T'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while((c = getopt_long(argc, argv, "aA:c:d:t:o:O:sTlvh", long_options, NULL)) > -1) { switch (c) { case 'a': show_all_types = 1; break; case 'c': catalog_host = optarg; break; case 'd': debug_flags_set(optarg); break; case 't': timeout = string_time_parse(optarg); break; case 'A': minavail = string_metric_parse(optarg); break; case 'o': debug_config_file(optarg); break; case 'O': debug_config_file_size(string_metric_parse(optarg)); break; case 'v': cctools_version_print(stdout, argv[0]); return 1; case 's': mode = MODE_SHORT; break; case 'l': mode = MODE_LONG; break; case 'T': mode = MODE_TOTAL; break; case 'h': default: show_help(argv[0]); return 1; } } cctools_version_debug(D_DEBUG, argv[0]); if(argc - optind == 0) { // fine, keep going } else if((argc - optind) == 1) { filter_name = "name"; filter_value = argv[optind]; } else if((argc - optind) == 2) { filter_name = argv[optind]; filter_value = argv[optind + 1]; } else { show_help(argv[0]); return 1; } stoptime = time(0) + timeout; q = catalog_query_create(catalog_host, 0, stoptime); if(!q) { fprintf(stderr, "couldn't query catalog: %s\n", strerror(errno)); return 1; } if(mode == MODE_TABLE) { nvpair_print_table_header(stdout, headers); } while((n = catalog_query_read(q, stoptime))) { table[count++] = n; } qsort(table, count, sizeof(*table), (int (*)(const void *, const void *)) compare_entries); for(i = 0; i < count; i++) { const char *etype = nvpair_lookup_string(table[i], "type"); if(!show_all_types) { if(etype) { if(!strcmp(etype, "chirp") || !strcmp(etype, "catalog")) { /* ok, keep going */ } else { continue; } } else { continue; } } if(minavail != 0) { if(minavail > nvpair_lookup_integer(table[i], "avail")) { continue; } } if(filter_name) { const char *v = nvpair_lookup_string(table[i], filter_name); if(!v || strcmp(filter_value, v)) continue; } if(mode == MODE_SHORT) { const char *t = nvpair_lookup_string(table[i], "type"); if(t && !strcmp(t, "chirp")) { printf("%s:%d\n", nvpair_lookup_string(table[i], "name"), (int) nvpair_lookup_integer(table[i], "port")); } } else if(mode == MODE_LONG) { nvpair_print_text(table[i], stdout); } else if(mode == MODE_TABLE) { nvpair_print_table(table[i], stdout, headers); } else if(mode == MODE_TOTAL) { avail += nvpair_lookup_integer(table[i], "avail"); total += nvpair_lookup_integer(table[i], "total"); } } if(mode == MODE_TOTAL) { printf("NODES: %4d\n", count); printf("TOTAL: %6sB\n", string_metric(total, -1, 0)); printf("AVAIL: %6sB\n", string_metric(avail, -1, 0)); printf("INUSE: %6sB\n", string_metric(total - avail, -1, 0)); } if(mode == MODE_TABLE) { nvpair_print_table_footer(stdout, headers); } return 0; }
static void handle_query(struct link *query_link) { FILE *stream; char line[LINE_MAX]; char url[LINE_MAX]; char path[LINE_MAX]; char action[LINE_MAX]; char version[LINE_MAX]; char hostport[LINE_MAX]; char addr[LINK_ADDRESS_MAX]; char key[LINE_MAX]; int port; time_t current; char *hkey; struct nvpair *nv; int i, n; link_address_remote(query_link, addr, &port); debug(D_DEBUG, "www query from %s:%d", addr, port); if(link_readline(query_link, line, LINE_MAX, time(0) + HANDLE_QUERY_TIMEOUT)) { string_chomp(line); if(sscanf(line, "%s %s %s", action, url, version) != 3) { return; } // Consume the rest of the query while(1) { if(!link_readline(query_link, line, LINE_MAX, time(0) + HANDLE_QUERY_TIMEOUT)) { return; } if(line[0] == 0) { break; } } } else { return; } // Output response stream = fdopen(link_fd(query_link), "w"); if(!stream) { return; } link_nonblocking(query_link, 0); current = time(0); fprintf(stream, "HTTP/1.1 200 OK\n"); fprintf(stream, "Date: %s", ctime(¤t)); fprintf(stream, "Server: catalog_server\n"); fprintf(stream, "Connection: close\n"); if(sscanf(url, "http://%[^/]%s", hostport, path) == 2) { // continue on } else { strcpy(path, url); } /* load the hash table entries into one big array */ n = 0; nvpair_database_firstkey(table); while(nvpair_database_nextkey(table, &hkey, &nv)) { array[n] = nv; n++; } /* sort the array by name before displaying */ qsort(array, n, sizeof(struct nvpair *), compare_nvpair); if(!strcmp(path, "/query.text")) { fprintf(stream, "Content-type: text/plain\n\n"); for(i = 0; i < n; i++) nvpair_print_text(array[i], stream); } else if(!strcmp(path, "/query.json")) { fprintf(stream, "Content-type: text/plain\n\n"); fprintf(stream,"[\n"); for(i = 0; i < n; i++) { nvpair_print_json(array[i], stream); fprintf(stream,",\n"); } fprintf(stream,"]\n"); } else if(!strcmp(path, "/query.oldclassads")) { fprintf(stream, "Content-type: text/plain\n\n"); for(i = 0; i < n; i++) nvpair_print_old_classads(array[i], stream); } else if(!strcmp(path, "/query.newclassads")) { fprintf(stream, "Content-type: text/plain\n\n"); for(i = 0; i < n; i++) nvpair_print_new_classads(array[i], stream); } else if(!strcmp(path, "/query.xml")) { fprintf(stream, "Content-type: text/xml\n\n"); fprintf(stream, "<?xml version=\"1.0\" standalone=\"yes\"?>\n"); fprintf(stream, "<catalog>\n"); for(i = 0; i < n; i++) nvpair_print_xml(array[i], stream); fprintf(stream, "</catalog>\n"); } else if(sscanf(path, "/detail/%s", key) == 1) { struct nvpair *nv; fprintf(stream, "Content-type: text/html\n\n"); nv = nvpair_database_lookup(table, key); if(nv) { const char *name = nvpair_lookup_string(nv, "name"); if(!name) name = "unknown"; fprintf(stream, "<title>%s catalog server: %s</title>\n", preferred_hostname, name); fprintf(stream, "<center>\n"); fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname); fprintf(stream, "<h2>%s</h2>\n", name); fprintf(stream, "<p><a href=/>return to catalog view</a><p>\n"); nvpair_print_html_solo(nv, stream); fprintf(stream, "</center>\n"); } else { fprintf(stream, "<title>%s catalog server</title>\n", preferred_hostname); fprintf(stream, "<center>\n"); fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname); fprintf(stream, "<h2>Unknown Item!</h2>\n"); fprintf(stream, "</center>\n"); } } else { char avail_line[LINE_MAX]; char total_line[LINE_MAX]; INT64_T sum_total = 0; INT64_T sum_avail = 0; INT64_T sum_devices = 0; fprintf(stream, "Content-type: text/html\n\n"); fprintf(stream, "<title>%s catalog server</title>\n", preferred_hostname); fprintf(stream, "<center>\n"); fprintf(stream, "<h1>%s catalog server</h1>\n", preferred_hostname); fprintf(stream, "<a href=/query.text>text</a> - "); fprintf(stream, "<a href=/query.html>html</a> - "); fprintf(stream, "<a href=/query.xml>xml</a> - "); fprintf(stream, "<a href=/query.json>json</a> - "); fprintf(stream, "<a href=/query.oldclassads>oldclassads</a> - "); fprintf(stream, "<a href=/query.newclassads>newclassads</a>"); fprintf(stream, "<p>\n"); for(i = 0; i < n; i++) { nv = array[i]; sum_total += nvpair_lookup_integer(nv, "total"); sum_avail += nvpair_lookup_integer(nv, "avail"); sum_devices++; } string_metric(sum_avail, -1, avail_line); string_metric(sum_total, -1, total_line); fprintf(stream, "<b>%sB available out of %sB on %d devices</b><p>\n", avail_line, total_line, (int) sum_devices); nvpair_print_html_header(stream, html_headers); for(i = 0; i < n; i++) { nv = array[i]; make_hash_key(nv, key); sprintf(url, "/detail/%s", key); nvpair_print_html_with_link(nv, stream, html_headers, "name", url); } nvpair_print_html_footer(stream, html_headers); fprintf(stream, "</center>\n"); } fclose(stream); }
static void handle_updates(struct datagram *update_port) { char data[DATAGRAM_PAYLOAD_MAX * 2]; char addr[DATAGRAM_ADDRESS_MAX]; char key[LINE_MAX]; int port; int result; struct nvpair *nv; while(1) { result = datagram_recv(update_port, data, DATAGRAM_PAYLOAD_MAX, addr, &port, 0); if(result <= 0) return; data[result] = 0; nv = nvpair_create(); nvpair_parse(nv, data); nvpair_insert_string(nv, "address", addr); nvpair_insert_integer(nv, "lastheardfrom", time(0)); /* If the server reports unbelievable numbers, simply reset them */ if(max_server_size > 0) { INT64_T total = nvpair_lookup_integer(nv, "total"); INT64_T avail = nvpair_lookup_integer(nv, "avail"); if(total > max_server_size || avail > max_server_size) { nvpair_insert_integer(nv, "total", max_server_size); nvpair_insert_integer(nv, "avail", max_server_size); } } /* Do not believe the server's reported name, just resolve it backwards. */ char name[DOMAIN_NAME_MAX]; if(domain_name_cache_lookup_reverse(addr, name)) { nvpair_insert_string(nv, "name", name); } else if (nvpair_lookup_string(nv, "name") == NULL) { /* If rDNS is unsuccessful, then we use the name reported if given. * This allows for hostnames that are only valid in the subnet of * the reporting server. Here we set the "name" field to the IP * Address, addr, because it was not set by the reporting server. */ nvpair_insert_string(nv, "name", addr); } make_hash_key(nv, key); if(logfile) { if(!nvpair_database_lookup(table,key)) { nvpair_print_text(nv,logfile); fflush(logfile); } } nvpair_database_insert(table, key, nv); debug(D_DEBUG, "received udp update from %s", key); } }
static int log_play( struct deltadb *db ) { FILE *stream = stdin; time_t current = 0; struct nvpair *nv; int line_number = 0; struct hash_table *table = db->table; char line[NVPAIR_LINE_MAX]; char key[NVPAIR_LINE_MAX]; char name[NVPAIR_LINE_MAX]; char value[NVPAIR_LINE_MAX]; char oper; int notime = 1; while(fgets(line,sizeof(line),stream)) { line_number += 1; int keep = 0; if (line[0]=='.') return 0; int n = sscanf(line,"%c %s %s %[^\n]",&oper,key,name,value); if(n<1) continue; switch(oper) { case 'C': nv = nvpair_create(); int res = nvpair_parse_stream(nv,stream); if (res>0){ keep = 1; struct argument *arg = db->args; while (arg!=NULL){ const char *var = nvpair_lookup_string(nv,arg->param); if ( var!=NULL && keep_object(arg,var)==0 ){ keep = 0; break; } arg = arg->next; } nvpair_delete(hash_table_remove(db->table,key)); if (keep==1){ if (notime){ printf("T %lld\n",(long long)current); notime = 0; } nvpair_print_text(nv,stdout); hash_table_insert(db->table,key,nv); } } if (!keep) nvpair_delete(nv); break; case 'D': nv = hash_table_remove(table,key); if(nv){ if (notime){ printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); }//nvpair_delete(nv); break; case 'U': nv = hash_table_lookup(table,key); if(nv){ if (notime){ printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); }//nvpair_insert_string(nv,name,value); break; case 'R': nv = hash_table_lookup(table,key); if(nv){ if (notime){ printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); }//nvpair_remove(nv,name); break; case 'T': current = atol(key); notime = 1; break; default: debug(D_NOTICE,"corrupt log data[%i]: %s",line_number,line); fflush(stderr); break; } } return 1; }
static int log_play( struct deltadb *db ) { FILE *stream = stdin; time_t current = 0; struct nvpair *nv; int line_number = 0; char line[NVPAIR_LINE_MAX]; char key[NVPAIR_LINE_MAX]; char name[NVPAIR_LINE_MAX]; char value[NVPAIR_LINE_MAX]; char oper; int notime = 1; while(fgets(line,sizeof(line),stream)) { line_number += 1; if (line[0]=='\n') return 0; int n = sscanf(line,"%c %s %s %[^\n]",&oper,key,name,value); if(n<1) continue; int i,include; switch(oper) { case 'C': nv = nvpair_create(); int num_pairs = nvpair_parse_stream_limited(nv,stream,db->attr_list,db->attr_len); if (num_pairs>0) { if (notime) { printf("T %lld\n",(long long)current); notime = 0; } printf("C %s\n",key); nvpair_print_text(nv,stdout); } nvpair_delete(nv); break; case 'D': if (notime) { printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); break; case 'U': include = 0; for(i=0; i<db->attr_len; i++) { if(strcmp(name,db->attr_list[i])==0) { include = 1; break; } } if (include>0) { if (notime) { printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); } break; case 'R': include = 0; for(i=0; i<db->attr_len; i++) { if(strcmp(name,db->attr_list[i])==0) { include = 1; break; } } if (include>0) { if (notime) { printf("T %lld\n",(long long)current); notime = 0; } printf("%s",line); } break; case 'T': current = atol(key); notime = 1; break; default: debug(D_NOTICE,"corrupt log data[%i]: %s",line_number,line); fflush(stderr); break; } } return 0; }