Project

General

Profile

Feature #2432 » mod_status-json.patch

gstrauss, 2016-04-21 09:19

View differences:

src/mod_status.c
655 655
	return 0;
656 656
}
657 657

  
658

  
659
static handler_t mod_status_handle_server_status_json(server *srv, connection *con, void *p_d) {
660
	plugin_data *p = p_d;
661
	buffer *b = buffer_init();
662
	double avg;
663
	time_t ts;
664
	char buf[32];
665
	size_t j;
666
	unsigned int jsonp = 0;
667

  
668
	if (buffer_string_length(con->uri.query) >= sizeof("jsonp=")-1
669
	    && 0 == memcmp(con->uri.query->ptr, CONST_STR_LEN("jsonp="))) {
670
		/* not a full parse of query string for multiple parameters,
671
		 * not URL-decoding param and not XML-encoding (XSS protection),
672
		 * so simply ensure that json function name isalnum() or '_' */
673
		const char *f = con->uri.query->ptr + sizeof("jsonp=")-1;
674
		int len = 0;
675
		while (light_isalnum(f[len]) || f[len] == '_') ++len;
676
		if (0 != len && light_isalpha(f[0]) && f[len] == '\0') {
677
			buffer_append_string_len(b, f, len);
678
			buffer_append_string_len(b, CONST_STR_LEN("("));
679
			jsonp = 1;
680
		}
681
	}
682

  
683
	/* output total number of requests */
684
	buffer_append_string_len(b, CONST_STR_LEN("{\n\t\"RequestsTotal\": "));
685
	avg = p->abs_requests;
686
	snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
687
	buffer_append_string(b, buf);
688
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
689

  
690
	/* output total traffic out in kbytes */
691
	buffer_append_string_len(b, CONST_STR_LEN("\t\"TrafficTotal\": "));
692
	avg = p->abs_traffic_out / 1024;
693
	snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
694
	buffer_append_string(b, buf);
695
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
696

  
697
	/* output uptime */
698
	buffer_append_string_len(b, CONST_STR_LEN("\t\"Uptime\": "));
699
	ts = srv->cur_ts - srv->startup_ts;
700
	buffer_append_int(b, ts);
701
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
702

  
703
	/* output busy servers */
704
	buffer_append_string_len(b, CONST_STR_LEN("\t\"BusyServers\": "));
705
	buffer_append_int(b, srv->conns->used);
706
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
707

  
708
	buffer_append_string_len(b, CONST_STR_LEN("\t\"IdleServers\": "));
709
	buffer_append_int(b, srv->conns->size - srv->conns->used);
710
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
711

  
712
	for (j = 0, avg = 0; j < 5; j++) {
713
		avg += p->mod_5s_requests[j];
714
	}
715

  
716
	avg /= 5;
717

  
718
	buffer_append_string_len(b, CONST_STR_LEN("\t\"RequestAverage5s\":"));
719
	buffer_append_int(b, avg);
720
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
721

  
722
	for (j = 0, avg = 0; j < 5; j++) {
723
		avg += p->mod_5s_traffic_out[j];
724
	}
725

  
726
	avg /= 5;
727

  
728
	buffer_append_string_len(b, CONST_STR_LEN("\t\"TrafficAverage5s\":"));
729
	buffer_append_int(b, avg / 1024); /* kbps */
730
	buffer_append_string_len(b, CONST_STR_LEN("\n}"));
731

  
732
	if (jsonp) buffer_append_string_len(b, CONST_STR_LEN(");"));
733

  
734
	chunkqueue_append_buffer(con->write_queue, b);
735
	buffer_free(b);
736

  
737
	/* set text/plain output */
738
	response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/javascript"));
739

  
740
	return 0;
741
}
742

  
743

  
658 744
static handler_t mod_status_handle_server_statistics(server *srv, connection *con, void *p_d) {
659 745
	buffer *b;
660 746
	size_t i;
......
695 781

  
696 782
	if (buffer_is_equal_string(con->uri.query, CONST_STR_LEN("auto"))) {
697 783
		mod_status_handle_server_status_text(srv, con, p_d);
784
	} else if (buffer_string_length(con->uri.query) >= sizeof("json")-1
785
		   && 0 == memcmp(con->uri.query->ptr, CONST_STR_LEN("json"))) {
786
		mod_status_handle_server_status_json(srv, con, p_d);
698 787
	} else {
699 788
		mod_status_handle_server_status_html(srv, con, p_d);
700 789
	}
(2-2/2)