Código:
var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var sendDat = getXmlHttpRequestObject(); var lastMessage = 0; var mTimer; //Función para inicializar la página. function startSOC() { //Iniciar recibiendo Logs. getLogs(); } //Obtener los objetos XmlHttpRequest especificos del navegador. function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('estado').innerHTML = 'No se puede crear el objeto XmlHttpRequest.'; } } //Obtener los mensajes desde el servidor Syslog-ng function getLogs() { receiveReq.open("GET", 'Logs.php?logs=1&last=' + lastMessage, true); receiveReq.onreadystatechange = ReceiveLogs; receiveReq.send(null); } //Funcion para obtener los mensajes de logs. function ReceiveLogs() { var logs_div = document.getElementById('div_logs'); var xmldoc = receiveReq.responseXML; var message_nodes = xmldoc.getElementsByTagName("message"); var n_messages = message_nodes.length for (i = 0; i < n_messages; i++) { var seq_node = message_nodes[i].getElementsByTagName("seq"); var host_node = message_nodes[i].getElementsByTagName("host"); var priority_node = message_nodes[i].getElementsByTagName("priority"); var date_node = message_nodes[i].getElementsByTagName("date"); var time_node = message_nodes[i].getElementsByTagName("time"); var id_node = message_nodes[i].getElementsByTagName("id"); var src_node = message_nodes[i].getElementsByTagName("src"); var interface_node = message_nodes[i].getElementsByTagName("interface"); var interfaced_node = message_nodes[i].getElementsByTagName("interfaced"); var dst_node = message_nodes[i].getElementsByTagName("dst"); var seq = seq_node[0].firstChild.nodeValue; //Inserta el valor de los nodos en logs_div logs_div.innerHTML += "<div id='host'>" + host_node[0].firstChild.nodeValue + "</div>"; //Selección de color para el campo Prioridad switch(priority_node[0].firstChild.nodeValue){ case 'emergency': logs_div.innerHTML += "<div id='priority1'>Emergencia</div>"; break; case 'alert': logs_div.innerHTML += "<div id='priority2'>Alerta</div>"; break; case 'critical': logs_div.innerHTML += "<div id='priority3'>Critico</div>"; break; case 'error': logs_div.innerHTML += "<div id='priority4'>Eror</div>"; break; case 'warning': logs_div.innerHTML += "<div id='priority5'>Advertencia</div>"; break; case 'notice': logs_div.innerHTML += "<div id='priority6'>Notificación</div>"; break; case 'information': logs_div.innerHTML += "<div id='priority7'>Información</div>"; break; case 'debug': logs_div.innerHTML += "<div id='priority8'>Depuración</div>"; break; } logs_div.innerHTML += "<div id='date'>" + date_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='time'>" + time_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='id'>" + id_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='src'>" + src_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='interface'>" + interface_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='interfaced'>" + interfaced_node[0].firstChild.nodeValue + "</div>"; logs_div.innerHTML += "<div id='dst'>" + dst_node[0].firstChild.nodeValue + "</div>"; logs_div.scrollTop = logs_div.scrollHeight; lastMessage = (message_nodes[i].getAttribute('id')); } mTimer = setTimeout('Logs();',1000); //Verifica cambios en la base de datos cada segundo. }