static unsigned int vstat_reply(struct http_request *request, const char *arg, void *data) { struct vstat_priv_t *vstat; struct agent_core_t *core = data; struct http_response *resp; (void)arg; GET_PRIV(core, vstat); if (check_reopen(&vstat->http)) { http_reply(request->connection, 500, "Couldn't open shmlog"); return 0; } do_json(vstat->http.vd, vstat->http.vsb); resp = http_mkresp(request->connection, 200, NULL); resp->data = VSB_data(vstat->http.vsb); resp->ndata = VSB_len(vstat->http.vsb); http_add_header(resp,"Content-Type","application/json"); send_response(resp); http_free_resp(resp); VSB_clear(vstat->http.vsb); return 0; }
bool run(void) { acl::string body; // 从本地文件中加载请求数据 if (acl::ifstream::load(file_, &body) == false) { logger_error("load %s error", file_.c_str()); return false; } // 创建 HTTP 请求客户端 acl::http_request req(server_addr_); // 添加 HTTP 请求头字段 acl::string ctype("text/"); ctype << stype_ << "; charset=" << charset_; acl::http_header& hdr = req.request_header(); // 请求头对象的引用 hdr.set_url("/"); hdr.set_content_type(ctype); // 发送 HTTP 请求数据 if (req.request(body.c_str(), body.length()) == false) { logger_error("send http request to %s error", server_addr_.c_str()); return false; } // 取出 HTTP 响应头的数据字段 const char* p = req.header_value("Content-Type"); if (p == NULL || *p == 0) { logger_error("no Content-Type"); return false; } // 分析 HTTP 响应头的数据类型 acl::http_ctype content_type; content_type.parse(p); // 响应头数据类型的子类型 const char* stype = content_type.get_stype(); bool ret; if (stype == NULL) ret = do_plain(req); else if (strcasecmp(stype, "xml") == 0) ret = do_xml(req); else if (strcasecmp(stype, "json") == 0) ret = do_json(req); else ret = do_plain(req); if (ret == true) printf("read ok!\r\n"); return ret; }
/* * Push stats to url. * * Called from different threads due to /push/test/stats */ static int push_stats(struct vstat_priv_t *vstat, struct vstat_thread_ctx_t *ctx) { struct ipc_ret_t vret; int ret = 0; pthread_rwlock_rdlock(&vstat->lck); if (!vstat->push_url || !(*vstat->push_url) || check_reopen(ctx)) { pthread_rwlock_unlock(&vstat->lck); return -1; } do_json(ctx->vd, ctx->vsb); ipc_run(ctx->curl, &vret, "%s\n%s",vstat->push_url, VSB_data(ctx->vsb)); pthread_rwlock_unlock(&vstat->lck); VSB_clear(ctx->vsb); if (vret.status != 200) { logger(ctx->logger,"cURL returned %d: %s", vret.status, vret.answer); ret = -1; } assert(vret.answer); free(vret.answer); return ret; }
int main(int argc, char * const *argv) { int c; struct VSM_data *vd; int delay = 1, once = 0, xml = 0, json = 0, do_repeat = 0; vd = VSM_New(); while ((c = getopt(argc, argv, VSC_ARGS "1f:lVw:xjt:")) != -1) { switch (c) { case '1': once = 1; break; case 'l': list_fields(vd); exit(0); case 'V': VCS_Message("varnishstat"); exit(0); case 'w': do_repeat = 1; delay = atoi(optarg); break; case 'x': xml = 1; break; case 'j': json = 1; break; default: if (VSC_Arg(vd, c, optarg) > 0) break; usage(); } } if (VSM_Open(vd)) { fprintf(stderr, "%s\n", VSM_Error(vd)); exit(1); } if (!(xml || json || once)) { do_curses(vd, delay); exit(0); } while (1) { if (xml) do_xml(vd); else if (json) do_json(vd); else if (once) do_once(vd, VSC_Main(vd, NULL)); else { assert(0); } if (!do_repeat) break; // end of output block marker. printf("\n"); sleep(delay); } exit(0); }
int main(int argc, char * const *argv) { struct VSM_data *vd; double t_arg = 5.0, t_start = NAN; int once = 0, xml = 0, json = 0, f_list = 0, curses = 0; signed char opt; int i; VUT_Init(progname, argc, argv, &vopt_spec); vd = VSM_New(); AN(vd); while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) { switch (opt) { case '1': once = 1; break; case 'h': /* Usage help */ usage(0); case 'l': f_list = 1; break; case 't': if (!strcasecmp(optarg, "off")) t_arg = -1.; else { t_arg = VNUM(optarg); if (isnan(t_arg)) VUT_Error(1, "-t: Syntax error"); if (t_arg < 0.) VUT_Error(1, "-t: Range error"); } break; case 'V': VCS_Message("varnishstat"); exit(0); case 'x': xml = 1; break; case 'j': json = 1; break; default: i = VSC_Arg(vd, opt, optarg); if (i < 0) VUT_Error(1, "%s", VSM_Error(vd)); if (!i) usage(1); } } if (optind != argc) usage(1); if (!(xml || json || once || f_list)) curses = 1; while (1) { i = VSM_Open(vd); if (!i) break; if (isnan(t_start) && t_arg > 0.) { fprintf(stderr, "Can't open log -" " retrying for %.0f seconds\n", t_arg); t_start = VTIM_real(); } if (t_arg <= 0.) break; if (VTIM_real() - t_start > t_arg) break; VSM_ResetError(vd); VTIM_sleep(0.5); } if (curses) { if (i && t_arg >= 0.) VUT_Error(1, "%s", VSM_Error(vd)); do_curses(vd, 1.0); exit(0); } if (i) VUT_Error(1, "%s", VSM_Error(vd)); if (xml) do_xml(vd); else if (json) do_json(vd); else if (once) do_once(vd); else if (f_list) list_fields(vd); else assert(0); exit(0); }