Project

General

Profile

Feature #2432 » mod_status.patch

Xaos, 2012-08-07 17:57

View differences:

lighttpd-1.4.32-patch/src/mod_status.c 2012-08-07 17:25:05.000000000 +0200
620 620
	return 0;
621 621
}
622 622

  
623
/* JSON TEXT */
624
static handler_t mod_status_handle_server_status_json(server *srv, connection *con, void *p_d) {
625
	plugin_data *p = p_d;
626
	buffer *b;
627
	double avg;
628
	time_t ts;
629
	char buf[32];
630
	size_t j;
631
	unsigned char jsonp = 0;
632

  
633
	b = chunkqueue_get_append_buffer(con->write_queue);
634

  
635
	/* check for jsonp request - 8 byte check: "jsonp=" + min 1 char callback function name + termination */
636
	if (con->uri.query->used >= 8 && buffer_search_string_len(con->uri.query, CONST_STR_LEN("jsonp")) == con->uri.query->ptr){
637
		/* extract function name from uri.query an append it to ouput buffer */
638
		buffer_append_memory(b, con->uri.query->ptr+6, con->uri.query->used-6);
639

  
640
		/* function start */
641
		buffer_append_string_len(b, CONST_STR_LEN("("));
642

  
643
		/* set flag */
644
		jsonp = 1;
645
	}
646

  
647
	/* output total number of requests */
648
	buffer_append_string_len(b, CONST_STR_LEN("{\n\t\"RequestsTotal\": "));
649
	avg = p->abs_requests;
650
	snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
651
	buffer_append_string(b, buf);
652
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
653

  
654
	/* output total traffic out in kbytes */
655
	buffer_append_string_len(b, CONST_STR_LEN("\t\"TrafficTotal\": "));
656
	avg = p->abs_traffic_out / 1024;
657
	snprintf(buf, sizeof(buf) - 1, "%.0f", avg);
658
	buffer_append_string(b, buf);
659
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
660

  
661
	/* output uptime */
662
	buffer_append_string_len(b, CONST_STR_LEN("\t\"Uptime\": "));
663
	ts = srv->cur_ts - srv->startup_ts;
664
	buffer_append_long(b, ts);
665
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
666

  
667
	/* output busy servers */
668
	buffer_append_string_len(b, CONST_STR_LEN("\t\"BusyServers\": "));
669
	buffer_append_long(b, srv->conns->used);
670
	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
671

  
672
	buffer_append_string_len(b, CONST_STR_LEN("\t\"IdleServers\": "));
673
    buffer_append_long(b, srv->conns->size - srv->conns->used);
674
    buffer_append_string_len(b, CONST_STR_LEN(",\n"));
675

  
676
    /* average request last 5s */
677
   	for (j = 0, avg = 0; j < 5; j++) {
678
   		avg += p->mod_5s_requests[j];
679
   	}
680
   	avg /= 5;
681

  
682
   	buffer_append_string_len(b, CONST_STR_LEN("\t\"RequestsAverage5s\": "));
683
   	buffer_append_long(b, avg);
684
   	buffer_append_string_len(b, CONST_STR_LEN(",\n"));
685

  
686
    /* average traffic last 5s */
687
   	for (j = 0, avg = 0; j < 5; j++) {
688
   		avg += p->mod_5s_traffic_out[j] / 1024;
689
   	}
690
   	avg /= 5;
691

  
692
   	buffer_append_string_len(b, CONST_STR_LEN("\t\"TrafficAverage5s\": "));
693
   	sprintf(buf, "%.2f", avg);
694
   	buffer_append_string(b, buf);
695
   	buffer_append_string_len(b, CONST_STR_LEN("\n}"));
696

  
697
   	/* jsonp ending */
698
   	if (jsonp == 1){
699
   		buffer_append_string_len(b, CONST_STR_LEN(");"));
700
   	}
701

  
702
	/* set text/plain output */
703
	response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/javascript"));
704

  
705
	return 0;
706
}
707

  
623 708
static handler_t mod_status_handle_server_statistics(server *srv, connection *con, void *p_d) {
624 709
	buffer *b;
625 710
	size_t i;
......
655 740

  
656 741

  
657 742
static handler_t mod_status_handle_server_status(server *srv, connection *con, void *p_d) {
658

  
743
	/* simple text based output */
659 744
	if (buffer_is_equal_string(con->uri.query, CONST_STR_LEN("auto"))) {
660 745
		mod_status_handle_server_status_text(srv, con, p_d);
746

  
747
	/* json output - search for query string starts with "json" to handle jsonp requests too */
748
	} else if (con->uri.query->ptr != NULL && buffer_search_string_len(con->uri.query, CONST_STR_LEN("json")) == con->uri.query->ptr) {
749
		mod_status_handle_server_status_json(srv, con, p_d);
750

  
751
	/* nice html output */
661 752
	} else {
662 753
		mod_status_handle_server_status_html(srv, con, p_d);
663 754
	}
(1-1/2)