void database_delete(struct Connection *conn, int id) { struct Address addr = {.id=id, .set=0}; conn->db->rows[id] = addr; } void database_list(struct Connection *conn) { for(int i=0; i<MAX_ROWS; ++i) { struct Address *addr = &conn->db->rows[i]; if(addr->set) address_print(addr); } } int main(int argc, char *argv[]) { if(argc < 3) die("usage: a.out file action additional_params"); char *filename = argv[1]; char action = argv[2][0]; struct Connection *conn = database_open(filename, action); int id=0; if(argc > 3) id = atoi(argv[3]); if(id >= MAX_ROWS) die("record id too high"); switch (action) { case 'c': database_create(conn); database_write(conn); break; case 'g': if(argc!=4) die("need an id for database_get()"); database_get(conn,id); break; case 's': if(argc!=6) die("need id, name, email for database_set()"); database_set(conn,id,argv[4],argv[5]); database_write(conn); break; case 'd': if(argc!=4) die("need id to delete"); database_delete(conn,id); database_write(conn); break; case 'l': database_list(conn); break; default: die("invalid action: c=create, g=get, s=set, d=delete, l=list"); } database_close(conn); return 0; }
/* Process a LIST command. */ yerr_t command_list(tcp_thread_t *thread, ybool_t sync, ybool_t compress, ybool_t serialized, ydynabin_t *buff) { char last_byte = 0; yerr_t result; YLOG_ADD(YLOG_DEBUG, "LIST command"); // send the response to the client result = CONNECTION_SEND_OK(thread); if (result != YENOERR) goto error; // send data if (database_list(thread->finedb->database, thread->transaction, thread->dbname, _command_list_loop, thread) != YENOERR) goto error; // send last byte if (write(thread->fd, &last_byte, 1) != 1) goto error; YLOG_ADD(YLOG_DEBUG, "LIST command OK"); return (result); error: YLOG_ADD(YLOG_WARN, "LIST error"); CONNECTION_SEND_ERROR(thread, RESP_ERR_SERVER); return (YEIO); }
void database_create(struct Connection *conn) { int i = 0; for(i = 0; i < MAX_ROWS; i++) { // make a prototype to initialize the database struct Address addr = {.id = i, .set = 0}; // then assign it to conn conn->db->rows[i] = addr; } printf("got to bottom of database create"); } void database_set(struct Connection *conn, int id, const char *name, const char *email) { struct Address *addr = &conn->db->rows[id]; if(addr->set) die("Already set, delete it first"); addr->set = 1; // WARNING FIX THIS char *res = strncpy(addr->name, name, MAX_DATA); // demonstrate that strncpy bug if(!res) die("Name copy failed"); res = strncpy(addr->email, email, MAX_DATA); if(!res) die("Email copy failed"); } void database_get(struct Connection *conn, int id) { struct Address *addr = &conn->db->rows[id]; if(addr->set) { address_print(addr); } else { die("ID is not set"); } } void database_delete(struct Connection *conn, int id) { struct Address addr = {.id = id, .set = 0}; conn->db->rows[id] = addr; } void database_list(struct Connection *conn) { int i = 0; struct Database *db = conn->db; for(i = 0; i < MAX_ROWS; i++) { struct Address *cur = &db->rows[i]; if(cur->set) { address_print(cur); } } } int main(int argc, char *argv[]) { if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]"); char *filename = argv[1]; char action = argv[2][0]; struct Connection *conn = database_open(filename, action); int id = 0; if(argc > 3) id = atoi(argv[3]); if(id >= MAX_ROWS) die("There's not that many records."); switch(action) { case 'c': database_create(conn); database_write(conn); break; case 'g': if(argc != 4) die("Need an id to get"); database_get(conn, id); break; case 's': if(argc != 6) die("Need id, name, email to set"); database_set(conn, id, argv[4], argv[5]); database_write(conn); break; case 'd': if(argc != 4) die("Need id to delete"); database_delete(conn, id); database_write(conn); break; case 'l': database_list(conn); break; default: die("Invalid action, only: c=create, g=get, s=set, d=del, l=list"); } database_close(conn); return 0; }
/* * This is the function called from the main polling loop. * * It calls command functions to get strings of data, and sends them to the server. * */ int collect_and_send_metrics(int cycle) { int retval; char* command; StringInfoData commands; pgstat_report_activity(STATE_RUNNING, "Collecting metrics"); initStringInfo(&commands); /* * Populate first cycle command data. These are executed on the first cycle * of a restart. The bgworker restars every N cycles, as listed at the bottom * of the main loop in pgsampler.c. */ if (cycle == 0) { command = restart_gucs(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("restart_gucs", command); pfree(command); command = relation_info(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_class", command); pfree(command); command = database_list(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("databases", command); pfree(command); command = column_info(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("columns", command); pfree(command); command = index_info(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("indexes", command); pfree(command); command = column_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_column", command); pfree(command); command = db_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_database", command); pfree(command); } /* HEARTBEAT */ if (cycle % heartbeat_seconds == 0) { command = heartbeat(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("heartbeats", command); pfree(command); } /* SYSTEM INFO */ if (cycle % system_seconds == 0) { command = system_info(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_system", command); pfree(command); command = fs_info(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_filesystem", command); pfree(command); } /* */ if (cycle % activity_seconds == 0) { command = activity_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_activity", command); pfree(command); } if (cycle % replication_seconds == 0) { command = replication_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_replication", command); pfree(command); } /* */ if (cycle % bgwriter_seconds == 0) { command = bgwriter_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_bgwriter", command); pfree(command); } if (cycle % guc_seconds == 0) { command = transient_gucs(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("transient_gucs", command); pfree(command); } if (cycle % statements_seconds == 0) { command = stat_statements(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_statements", command); pfree(command); } /* */ if (cycle % relation_seconds == 0) { command = table_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_table", command); pfree(command); command = index_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_index", command); pfree(command); command = table_io_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("statio_user_tables", command); pfree(command); command = index_io_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("statio_user_indexes", command); pfree(command); command = function_stats(); appendStringInfoString(&commands, command); if (strcmp(output_mode, "csv") == 0) write_to_csv("stat_function", command); pfree(command); } /* Send / Write metrics based on output_mode */ if (strcmp(output_mode, "network") == 0) { pgstat_report_activity(STATE_RUNNING, "Sending metrics to antenna"); retval = send_data(commands.data); if (retval == NO_DATA_SENT) { //close socket and retry establishing connection and sending data. // elog(LOG, "reseting..."); //just a note to say reseting socket if (sockfd != 0) shutdown(sockfd, SHUT_RDWR); sockfd = 0; retval = send_data(commands.data); // we ignore success or failure here. drops data if fails. } } return 0; }