ajax.js 5.21 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*   Most widgets update their backend data every 10 seconds.  11 seconds
 *   will ensure that we update the GUI right after the stats are updated.
 *   Seconds * 1000 = value
 */
var Seconds = 11;
var update_interval = (Math.abs(Math.ceil(Seconds))-1)*1000 + 990;

function updateMeters() {
	url = '/getstats.php';

	jQuery.ajax(url, {
		type: 'get',
		success: function(data) {
			response = data || "";
			if (response != "")
				stats(data);
		}
	});
        setTimer();
}

22 23
function setTimer() {
         timeout = window.setTimeout('updateMeters()', update_interval);
Ad Schellevis's avatar
Ad Schellevis committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
}

function stats(x) {
	var values = x.split("|");
	if (jQuery.each(values,function(key,value){
		if (value == 'undefined' || value == null)
			return true;
		else
			return false;
	}))

        updateUptime(values[2]);
        updateDateTime(values[5]);
        updateCPU(values[0]);
        updateMemory(values[1]);
        updateState(values[3]);
        updateTemp(values[4]);
        updateInterfaceStats(values[6]);
        updateInterfaces(values[7]);
        updateGatewayStats(values[8]);
        updateCpuFreq(values[9]);
        updateLoadAverage(values[10]);
        updateMbuf(values[11]);
        updateMbufMeter(values[12]);
        updateStateMeter(values[13]);
}

function updateMemory(x) {
	if(jQuery('#memusagemeter'))
		jQuery("#memusagemeter").html(x + '%');
	if(jQuery('#memUsagePB'))
Ad Schellevis's avatar
Ad Schellevis committed
55
		jQuery('#memUsagePB').css( { width: parseInt(x)+'%' } );
Ad Schellevis's avatar
Ad Schellevis committed
56 57 58 59 60 61 62 63 64 65 66
}

function updateMbuf(x) {
	if(jQuery('#mbuf'))
		jQuery("#mbuf").html(x);
}

function updateMbufMeter(x) {
	if(jQuery('#mbufusagemeter'))
		jQuery("#mbufusagemeter").html(x + '%');
	if(jQuery('#mbufPB'))
Ad Schellevis's avatar
Ad Schellevis committed
67
		jQuery('#mbufPB').css( { width: parseInt(x)+'%' } );
Ad Schellevis's avatar
Ad Schellevis committed
68 69 70 71 72 73
}

function updateCPU(x) {
	if(jQuery('#cpumeter'))
		jQuery("#cpumeter").html(x + '%');
	if(jQuery('#cpuPB'))
Ad Schellevis's avatar
Ad Schellevis committed
74
		jQuery('#cpuPB').css( { width: parseInt(x)+'%' } );
Ad Schellevis's avatar
Ad Schellevis committed
75 76 77 78 79 80
}

function updateTemp(x) {
	if(jQuery("#tempmeter"))
		jQuery("#tempmeter").html(x + '\u00B0' + 'C');
        if(jQuery('#tempPB'))
Ad Schellevis's avatar
Ad Schellevis committed
81
		jQuery("#tempPB").css( { width: parseInt(x)+'%' } );
Ad Schellevis's avatar
Ad Schellevis committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

function updateDateTime(x) {
	if(jQuery('#datetime'))
		jQuery("#datetime").html(x);
}

function updateUptime(x) {
	if(jQuery('#uptime'))
		jQuery("#uptime").html(x);
}

function updateState(x) {
	if(jQuery('#pfstate'))
		jQuery("#pfstate").html(x);
}

function updateStateMeter(x) {
	if(jQuery('#pfstateusagemeter'))
		jQuery("#pfstateusagemeter").html(x + '%');
	if(jQuery('#statePB'))
Ad Schellevis's avatar
Ad Schellevis committed
103
		jQuery('#statePB').css( { width: parseInt(x)+'%' } );
Ad Schellevis's avatar
Ad Schellevis committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

function updateGatewayStats(x){
	if (widgetActive("gateways")){
		gateways_split = x.split(",");
		for (var y=0; y<gateways_split.length; y++){
			if(jQuery('#gateway' + (y + 1))) {
				jQuery('#gateway' + (y + 1)).html(gateways_split[y]);
			}
		}
	}
}

function updateCpuFreq(x) {
	if(jQuery('#cpufreq'))
		jQuery("#cpufreq").html(x);
}

function updateLoadAverage(x) {
	if(jQuery('#load_average'))
		jQuery("#load_average").html(x);
}

function updateInterfaceStats(x){
	if (widgetActive("interface_statistics")){
		statistics_split = x.split(",");
		var counter = 1;
		for (var y=0; y<statistics_split.length-1; y++){
			if(jQuery('#stat' + counter)) {
				jQuery('#stat' + counter).html(statistics_split[y]);
134
				counter++;
Ad Schellevis's avatar
Ad Schellevis committed
135 136 137 138 139 140 141 142
			}
		}
	}
}

function updateInterfaces(x){
	if (widgetActive("interfaces")){
		interfaces_split = x.split("~");
Ad Schellevis's avatar
Ad Schellevis committed
143 144
		//interfaces_split.each(function(iface){
		jQuery.each(interfaces_split, function(id,iface){
Ad Schellevis's avatar
Ad Schellevis committed
145 146 147
			details = iface.split(",");
			switch(details[1]) {
				case "up":
Ad Schellevis's avatar
Ad Schellevis committed
148 149 150 151 152 153 154 155 156 157 158 159 160
					// Interface Arrow color
					jQuery('#' + details[0] ).addClass( "text-success" )
					jQuery('#' + details[0] ).removeClass( "text-danger" )

					// Interface Icon color
					jQuery('#' + details[0] + 'icon').addClass( "text-success" )
					jQuery('#' + details[0] + 'icon').removeClass( "text-danger" )

					// Interface Arrow type
					jQuery('#' + details[0] ).addClass( "glyphicon-arrow-up" )
					jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-down" )
					jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-remove" )

Ad Schellevis's avatar
Ad Schellevis committed
161 162
					break;
				case "down":
Ad Schellevis's avatar
Ad Schellevis committed
163 164 165 166 167 168 169 170 171 172 173 174
					jQuery('#' + details[0] ).addClass( "text-danger" )
					jQuery('#' + details[0] ).removeClass( "text-success" )

					// Interface Icon color
					jQuery('#' + details[0] + 'icon').addClass( "text-danger" )
					jQuery('#' + details[0] + 'icon').removeClass( "text-success" )

					// Interface Arrow type
					jQuery('#' + details[0] ).addClass( "glyphicon-arrow-down" )
					jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-up" )
					jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-remove" )

Ad Schellevis's avatar
Ad Schellevis committed
175 176
					break;
				case "block":
Ad Schellevis's avatar
Ad Schellevis committed
177 178 179 180 181 182 183 184
						// Interface Icon color
						jQuery('#' + details[0] ).addClass( "text-danger" )
						jQuery('#' + details[0] ).removeClass( "text-success" )

						// Interface Arrow type
						jQuery('#' + details[0] ).addClass( "glyphicon-arrow-remove" )
						jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-up" )
						jQuery('#' + details[0] ).removeClass( "glyphicon-arrow-down" )
Ad Schellevis's avatar
Ad Schellevis committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
					break;
			}
		});
	}
}

function widgetActive(x) {
	var widget = jQuery('#' + x + '-container');
	if ((widget != null) && (widget.css('display') != null) && (widget.css('display') != "none"))
		return true;
	else
		return false;
}

/* start updater */
jQuery(document).ready(function(){
	setTimer();
});