static int discover_mtu(void) { int seq = 1, low_mtu, high_mtu, try_mtu, r; /* Check if the host is up */ if (do_ping(seq++, 0) < 0) return -1; /* Discover PMTU */ low_mtu = 68/2; high_mtu = 1500/2; try_mtu = 1500/2; while (1) { r = do_ping(seq++, try_mtu * 2); if (r > 0 && r < try_mtu * 2) { /* pmtu */ high_mtu = r/2; try_mtu = high_mtu; continue; } if (r == 0) low_mtu = try_mtu; else high_mtu = try_mtu - 1; if (low_mtu >= high_mtu) return 2 * low_mtu; try_mtu = low_mtu + (high_mtu - low_mtu + 1) / 2; } }
//分析message函数, on_read之后调用 void talk_to_client::do_analyze(const std::string &msg) { if (msg.find(requests.request_register) == 0)do_register(msg); else if (msg.find(requests.request_login) == 0)do_login(msg); else if (msg.find(requests.request_ping) == 0)do_ping(); else if (msg.find(requests.request_ask) == 0)do_anw_clients(); else if (msg.find(requests.request_send_to) == 0)do_sendto(msg); else if (msg.find(requests.request_exit) == 0)do_exit(); else std::cout << msg << std::endl; return; }
///登陆回应处理 on_ 这部分代码改为成功之后将状态改写,提供接口给外部获得 void talk_to_svr::on_login(std::string &msg) { std::string answer; std::istringstream iss(msg); iss >> answer >> answer; if (answer.find("succeed") == 0) { _csignal = ok_login; do_ping(); } else /// 重新登录 return; }
static void server_callback (SoupServer *server, SoupMessage *msg, const char *path, GHashTable *query, SoupClientContext *context, gpointer data) { char *method_name; SoupXMLRPCParams *params; GError *error = NULL; if (msg->method != SOUP_METHOD_POST) { soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED); return; } soup_message_set_status (msg, SOUP_STATUS_OK); method_name = soup_xmlrpc_parse_request (msg->request_body->data, msg->request_body->length, ¶ms, &error); if (!method_name) { soup_xmlrpc_message_set_fault (msg, SOUP_XMLRPC_FAULT_PARSE_ERROR_NOT_WELL_FORMED, "Could not parse method call: %s", error->message); g_clear_error (&error); return; } if (!strcmp (method_name, "sum")) do_sum (msg, params); else if (!strcmp (method_name, "countBools")) do_countBools (msg, params); else if (!strcmp (method_name, "md5sum")) do_md5sum (msg, params); else if (!strcmp (method_name, "dateChange")) do_dateChange (msg, params); else if (!strcmp (method_name, "echo")) do_echo (msg, params); else if (!strcmp (method_name, "ping")) do_ping (msg, params); else { soup_xmlrpc_message_set_fault (msg, SOUP_XMLRPC_FAULT_SERVER_ERROR_REQUESTED_METHOD_NOT_FOUND, "Unknown method %s", method_name); } g_free (method_name); soup_xmlrpc_params_free (params); }
/****************************************************************************** * * * Function: process_pinger_hosts * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void process_pinger_hosts(icmpitem_t *items, int items_count) { const char *__function_name = "process_pinger_hosts"; int i, first_index = 0, ping_result; char error[ITEM_ERROR_LEN_MAX]; static ZBX_FPING_HOST *hosts = NULL; static int hosts_alloc = 4; int hosts_count = 0; zbx_timespec_t ts; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (NULL == hosts) hosts = zbx_malloc(hosts, sizeof(ZBX_FPING_HOST) * hosts_alloc); for (i = 0; i < items_count; i++) { add_pinger_host(&hosts, &hosts_alloc, &hosts_count, items[i].addr); if (i == items_count - 1 || items[i].count != items[i + 1].count || items[i].interval != items[i + 1].interval || items[i].size != items[i + 1].size || items[i].timeout != items[i + 1].timeout) { zbx_setproctitle("%s [pinging hosts]", get_process_type_string(process_type)); zbx_timespec(&ts); ping_result = do_ping(hosts, hosts_count, items[i].count, items[i].interval, items[i].size, items[i].timeout, error, sizeof(error)); process_values(items, first_index, i + 1, hosts, hosts_count, &ts, ping_result, error); hosts_count = 0; first_index = i + 1; } } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int main(int argc, char** argv) { printf("haltestelle built <%s %s> version %s\n", __DATE__, __TIME__, VERSION); if(argc < 5) { fprintf(stderr, "usage: %s <designation> <busfarhn_host> <busfarhn_port> <serialport>\n", argv[0]); exit(-1); } /*global*/ designation = argv[1]; /*global*/ busfarhn_host = argv[2]; /*global*/ busfarhn_port = atoi(argv[3]); char* serialport = argv[4]; /*global*/ sockfd = start_socket(); if(connect_to_busfarhn(designation) != 0) { fprintf(stderr, "[ERROR] connecting to %s:%d failed\n", busfarhn_host, busfarhn_port); return -1; } #ifdef USE_SERIAL #ifdef SERIAL_IS_A_FILE printf("%s is a FILE\n", serialport); #elif defined SERIAL_IS_A_ATH0 printf("%s is a SERIAL PORT (ATH style)\n", serialport); #else printf("%s is a SERIAL PORT\n", serialport); #endif #else printf("WARNING: NOT USING THE SERIAL PORT\n"); #endif printf("[STARTUP] connected to %s:%i with designation %s\n", busfarhn_host, busfarhn_port, designation); #ifdef USE_SERIAL #ifdef SERIAL_IS_A_FILE /*global*/ serialfd = fopen(serialport, "rw"); #elif defined SERIAL_IS_A_ATH0 /*global*/ serialfd = open(serialport, O_RDWR); #else /*global*/ serialfd = open(serialport, O_RDWR | O_NOCTTY | O_SYNC); #endif if (serialfd < 0) { fprintf(stderr, "error %d opening %s: %s", errno, serialport, strerror (errno)); return; } #if !(defined SERIAL_IS_A_FILE || defined SERIAL_IS_A_ATH0) set_interface_attribs (serialfd, SERIAL_BAUDRATE, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (serialfd, 0); // set no blocking #endif #endif hs_startup(); char netbuf[MAXBUFLEN]; unsigned int netbufpos = 0; struct sockaddr_storage their_addr; socklen_t addr_len; #ifdef USE_SERIAL pthread_t pth; pthread_create(&pth, NULL, thread_read_serial, NULL); #endif while(1) { // network int numbytes; addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd, (netbuf + netbufpos), MAXBUFLEN - (netbufpos + 1), MSG_DONTWAIT, (struct sockaddr *)&their_addr, &addr_len)) > 0) { printf("[NET] received %i bytes\n", numbytes); int i = 0; for(i = 0; i < numbytes; i++) if(netbuf[netbufpos + i] == '\n') break; netbufpos += numbytes; if(i < numbytes) { // found breakline netbuf[netbufpos - 1] = '\0'; netbufpos = 0; printf("CMD <%s>\n", netbuf); JSON_Value* json = json_parse_string(netbuf); if(json_value_get_type(json) == JSONObject) { JSON_Object* obj = json_value_get_object(json); const char* msgtype = json_object_dotget_string(obj, "type"); if(strcmp(msgtype, "io.tcp.ping") == 0) do_ping(); else do_network(json, netbuf); } json_value_free(json); } if(netbufpos + 1 == MAXBUFLEN) { printf("[NET] ERROR: received %i bytes and still no breakline. Discarding the message.\n", MAXBUFLEN); netbufpos = 0; } } else if(numbytes == 0) { break; } } hs_shutdown(); close(sockfd); return 0; }
/****************************************************************************** * * * Function: discover_service * * * * Purpose: check if service is available and update database * * * * Parameters: service type, ip address, port number * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static int discover_service(DB_DCHECK *dcheck, char *ip, int port, char *value) { const char *__function_name = "discover_service"; int ret = SUCCEED; char key[MAX_STRING_LEN], error[ITEM_ERROR_LEN_MAX]; const char *service = NULL; AGENT_RESULT result; DC_ITEM item; ZBX_FPING_HOST host; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); init_result(&result); *value = '\0'; switch (dcheck->type) { case SVC_SSH: service = "ssh"; break; case SVC_LDAP: service = "ldap"; break; case SVC_SMTP: service = "smtp"; break; case SVC_FTP: service = "ftp"; break; case SVC_HTTP: service = "http"; break; case SVC_POP: service = "pop"; break; case SVC_NNTP: service = "nntp"; break; case SVC_IMAP: service = "imap"; break; case SVC_TCP: service = "tcp"; break; case SVC_HTTPS: service = "https"; break; case SVC_TELNET: service = "telnet"; break; case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: case SVC_SNMPv3: case SVC_ICMPPING: break; default: ret = FAIL; break; } if (SUCCEED == ret) { alarm(CONFIG_TIMEOUT); switch (dcheck->type) { /* simple checks */ case SVC_SSH: case SVC_LDAP: case SVC_SMTP: case SVC_FTP: case SVC_HTTP: case SVC_POP: case SVC_NNTP: case SVC_IMAP: case SVC_TCP: case SVC_HTTPS: case SVC_TELNET: zbx_snprintf(key, sizeof(key), "net.tcp.service[%s,%s,%d]", service, ip, port); if (SUCCEED != process(key, 0, &result) || NULL == GET_UI64_RESULT(&result) || 0 == result.ui64) { ret = FAIL; } break; /* agent and SNMP checks */ case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: case SVC_SNMPv3: memset(&item, 0, sizeof(DC_ITEM)); strscpy(item.key_orig, dcheck->key_); item.key = item.key_orig; item.interface.useip = 1; item.interface.addr = ip; item.interface.port = port; item.value_type = ITEM_VALUE_TYPE_STR; switch (dcheck->type) { case SVC_SNMPv1: item.type = ITEM_TYPE_SNMPv1; break; case SVC_SNMPv2c: item.type = ITEM_TYPE_SNMPv2c; break; case SVC_SNMPv3: item.type = ITEM_TYPE_SNMPv3; break; default: item.type = ITEM_TYPE_ZABBIX; break; } if (SVC_AGENT == dcheck->type) { if (SUCCEED == get_value_agent(&item, &result) && NULL != GET_STR_RESULT(&result)) zbx_strlcpy(value, result.str, DSERVICE_VALUE_LEN_MAX); else ret = FAIL; } else #ifdef HAVE_SNMP { item.snmp_community = strdup(dcheck->snmp_community); item.snmp_oid = strdup(dcheck->key_); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmp_community, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_key_macros(&item.snmp_oid, NULL, NULL, NULL, MACRO_TYPE_SNMP_OID, NULL, 0); if (ITEM_TYPE_SNMPv3 == item.type) { item.snmpv3_securityname = strdup(dcheck->snmpv3_securityname); item.snmpv3_securitylevel = dcheck->snmpv3_securitylevel; item.snmpv3_authpassphrase = strdup(dcheck->snmpv3_authpassphrase); item.snmpv3_privpassphrase = strdup(dcheck->snmpv3_privpassphrase); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_securityname, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_authpassphrase, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_privpassphrase, MACRO_TYPE_ITEM_FIELD, NULL, 0); } if (SUCCEED == get_value_snmp(&item, &result) && NULL != GET_STR_RESULT(&result)) zbx_strlcpy(value, result.str, DSERVICE_VALUE_LEN_MAX); else ret = FAIL; zbx_free(item.snmp_community); zbx_free(item.snmp_oid); if (ITEM_TYPE_SNMPv3 == item.type) { zbx_free(item.snmpv3_securityname); zbx_free(item.snmpv3_authpassphrase); zbx_free(item.snmpv3_privpassphrase); } } #else ret = FAIL; #endif /* HAVE_SNMP */ if (FAIL == ret && ISSET_MSG(&result)) { zabbix_log(LOG_LEVEL_DEBUG, "discovery: item [%s] error: %s", item.key, result.msg); } break; case SVC_ICMPPING: memset(&host, 0, sizeof(host)); host.addr = strdup(ip); if (SUCCEED != do_ping(&host, 1, 3, 0, 0, 0, error, sizeof(error)) || 0 == host.rcv) ret = FAIL; zbx_free(host.addr); break; default: break; } alarm(0); } free_result(&result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
int main(int argc, char *argv[]) { #ifndef BS2000 extern char *optarg; #endif int c; int cd; int status; char remote_provider[16], remote_service[16]; int port = DEFAULT_PORT; struct ro_connect_rq rocrq; char event_buffer[EVENTBUFFERSIZE]; struct ro_event *roe = (struct ro_event *) event_buffer; strncpy(remote_provider,REMOTE_PROVIDER,15); strncpy(remote_service,REMOTE_SERVICE,15); remote_provider[15] = remote_service[15] = '\0'; #ifndef BS2000 while((c = getopt(argc, argv, "p:h:s:")) != -1) { switch(c) { case 'p': port = atoi(optarg); break; case 'h': strncpy(remote_provider,optarg,15); break; case 's': strncpy(remote_service,optarg,15); break; case '?': usage(); exit(1); } } #endif status = ro_init("roping.log",10,port,&roi); app_err(status,&roi); strncpy(rocrq.myhostname,MYHOSTNAME,15); strncpy(rocrq.myservicename,MYSERVICENAME,15); rocrq.level = RO_ADMIN_LEVEL; rocrq.roc.mode = RO_MODE_TRANSPARENT; rocrq.roc.escape = RO_DEFAULT_ESC; rocrq.id = 1; dprintf(1,"ro_ping: Connecting...\n"); status = ro_connect(remote_provider,remote_service,&rocrq,&cd,&roi); app_err(status,&roi); dprintf(1,"ro_ping: Waiting...\n"); while((status = ro_wait_event(-1, -1, sizeof(event_buffer), (struct ro_event *) event_buffer, &roi)) == RO_OK) { if(status != RO_OK) { app_err(status,&roi); break; } switch(roe->event_typ) { case RO_CONNECT_CONFIRMATION: cd = roe->cd; do_ping(cd); break; case RO_CONNECT_REJECT: puts("ro_ping: Connection rejected, exiting..."); exit(1); break; case RO_INVOKE_RESULT: printf("Pong (%lu) with %lu bytes received.\n", roe->event.roirs.id, roe->event.roirs.resultlen); sleep(1); do_ping(cd); break; case RO_INVOKE_REJECT: puts("ro_ping: Invocation rejected, exiting..."); exit(1); break; default: dprintf(2,"Unhandled event received (%d), ignoring...\n", roe->event_typ); break; } } ro_release(&roi); return 0; }