Skip to content

Commit

Permalink
Upgrade manifest version and other fixes
Browse files Browse the repository at this point in the history
* Upgrade manifest version to 2 so I can upload again to chrome store
* Fixes kepi#12 - In Icinga 1.9.x service list is paginated, added limit=0
* Fixes kepi#2 - caused by authentication problem
  • Loading branch information
kepi committed Mar 13, 2014
1 parent b21e31d commit f0ed4a7
Show file tree
Hide file tree
Showing 33 changed files with 1,139 additions and 1,173 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
.idea
243 changes: 4 additions & 239 deletions background.html
Original file line number Diff line number Diff line change
@@ -1,248 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<script src="inc/jquery-1.4.2.min.js"></script>
<script src="inc/jquery-2.1.0.min.js"></script>
<script src="icinga.js"></script>
<script src="inc/core-min.js"></script>
<script src="inc/enc-base64-min.js"></script>
<script src="inc/background.js"></script>
</head>
<body>

<script>
var hosts = undefined;
var error = undefined;
var refreshStatus = {state: false, error: undefined};

$("<div />").ajaxError(function() { debug_log('got ajax error'); });

function respond(resp, sendResponse)
{
if ( sendResponse != undefined )
sendResponse({state: resp.state, error: resp.error})

refreshStatus.state = resp.state;
refreshStatus.error = resp.error;
}

function refreshData(refreshStatus, sendResponse)
{
error = undefined;
refreshStatus.state = false;

// basic info from user setup
var username = window.localStorage.username;
var password = window.localStorage.password;
var url = window.localStorage.url;
var url_base = window.localStorage.url_base;

try {
var modifiers = window.localStorage.ignoreCaseSensitivity === "false" ? "" : "i";
var ignoreServicesRegexp = false;
if ( typeof(window.localStorage.ignoreServicesRegexp) != 'undefined' && window.localStorage.ignoreServicesRegexp != "" ) {
ignoreServicesRegexp = new RegExp (window.localStorage.ignoreServicesRegexp, modifiers);
}
var ignoreHostsRegexp = false;
if ( typeof(window.localStorage.ignoreHostsRegexp) != 'undefined' && window.localStorage.ignoreHostsRegexp != "" ) {
ignoreHostsRegexp = new RegExp (window.localStorage.ignoreHostsRegexp, modifiers);
}
} catch (e) {
error = 'bad-regexp';
respond({state: false, error: error}, sendResponse);
errorBadge();
return false;
}

// setup needs url
if ( url == '' || url == undefined ) {
debug_log('Url not defined');
error = 'need-setup';

// we need to handle response
respond({state: false, error: error}, sendResponse);

return false;
}

hosts = new Hosts();

// url is ok, add host=all for now
url = url+'?host=all&limit=0';

// try to get data from icinga
$.ajax({global: false, url: url, password: password, username: username, complete: function(res, status) {
//respond({state: false, error: 'au'}, sendResponse);
debug_log(res.status);
if ( res.status != 200 ) {
error = res.status == 404 ? 'bad-url' : 'unknown';
respond({state: false, error: error}, sendResponse);
return;
}

debug_log(res.getAllResponseHeaders());
if ( status === "success" || status === "notmodified" ) {
var selector = " .status > tbody > tr:gt(0)";

// regular pro smazani obrazku
//var rimg = /<img(.|\s)*?\/?>/gi;
var rimg = /<img.*?src='([^']+)'.*?\/?>/gi;

// nevim jestli by to neslo vic koser
var obj = $("<div />");
// TODO: potrebujeme ziskat url obrazku, tak neco lepsiho nez je smazat
obj.html($("<div />").append(res.responseText.replace(rimg, "<span imgurl='$1' >").replace(/<script.*?>.*?<\/script>/gmi, '').replace(/<script/gmi, '<oldscript')).find(selector));

var host = '';

var service = '';
var link = '';
var ackService = false;
var ackHost = false;
var downtimeService = false;
var downtimeHost = false;

// browse rows
obj.find("> tr").each( function(index, el) {
ack = false;
downtime = false;
// browse cols
$(el).find("> td").each( function(i, e) {
// first col, we solve host
if(i == 0) {
val = $(e).find("a").text();
if ( val != '' ) {
// get Host state
var statusclass = $(this).attr('class');
host = val;
var href = $(e).find("a").attr('href');
acked = $(e).find("span[imgurl*='ack.gif']").attr('imgurl');
ackHost = ( acked == undefined ) ? false : true;
downtimeed = $(e).find("span[imgurl*='downtime.gif']").attr('imgurl');
downtimeHost = ( downtimeed == undefined ) ? false : true;

// do not add hosts that we've ignored with regexp
if (typeof(ignoreHostsRegexp) != 'object' || host.search(ignoreHostsRegexp) == -1) {
h = new Host(host, href);
// TODO: pridat kontrolu acku u hosta
hosts.addHost(h, statusclass, ackHost, downtimeHost);
}
}
}
// second col is service
else if ( i == 1 ) {
service = $(e).find("a").text();
link = $(e).find("a").attr('href');
acked = $(e).find("span[imgurl*='ack.gif']").attr('imgurl');
ackService = ( acked == undefined ) ? false : true;
downtimeed = $(e).find("span[imgurl*='downtime.gif']").attr('imgurl');
downtimeService = ( downtimeed == undefined ) ? false : true;
}
// service status
else if ( i == 2 ) {
var state = $(e).text();

// do not add services that we've ignored with regexp
// also do not add those from ignored hosts
if ((typeof(ignoreServicesRegexp) != 'object' || service.search(ignoreServicesRegexp) == -1)
&& (typeof(ignoreHostsRegexp) != 'object' || host.search(ignoreHostsRegexp) == -1)) {
s = new Service(service, link, state, ackService, downtimeService);
hosts.getHost(host).addService(s);
//debug_log("Found service: "+service+" with url "+href+" and state "+state);
}
}
// other columns
// 3 - last check
// 4 - duration
// 5 - attempt
// 6 - status
});
});

hosts.setBadge();

respond({state: true, error: undefined}, sendResponse);
} else {
respond({state: false, error: 'unhandled state'}, sendResponse);
}

}});
}

var intervalId = undefined;

function setRefreshInterval()
{
// clear old interval
if ( intervalId == undefined )
clearInterval(intervalId);

// set refresh interval from options
var refreshInterval = window.localStorage.refresh;

if ( refreshInterval == undefined )
refreshInterval = 30000;
else
refreshInterval *= 1000;

// set refresh only if greater than 0
if ( refreshInterval > 0 ) {
debug_log("Refresh interval set to "+refreshInterval+" miliseconds");
intervalId = setInterval("refreshData(refreshStatus)", refreshInterval);
}
}

// TODO: locking aby se nemlatilo s rucnim reloadem
refreshData(refreshStatus);
setRefreshInterval();

// REQUEST LISTENER
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {

// function for reporting what background script supports
if(request.reqtype == 'supports') {
var state = 'no';

if ( request.what == 'dummy' )
state = 'ok';

sendResponse({state: state});

// get stored data
} else if ( request.reqtype == "get-data" ) {

// we need setup first
if ( error != undefined ) {
if ( sendResponse != undefined )
sendResponse({state: false, error: error});
return;
}
else if ( hosts == undefined) {
debug_log('Oooops, we have no data.');
sendResponse({state: false});
return;
}

resp = hosts.toJSON();
resp.state = refreshStatus.state;
resp.error = refreshStatus.error;
sendResponse(resp);

if ( refreshStatus.error )
clearBadge();

// refresh data
} else if ( request.reqtype == "refresh-data" ) {
refreshData(refreshStatus,sendResponse);
setRefreshInterval();

// reload background page
} else if(request.reqtype == 'reload-background') {
window.location.reload();

// unknown request
} else
sendResponse({state: false, error: 'Unknown request'});

});

</script>
</body>
</html>

2 changes: 1 addition & 1 deletion icinga.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,5 @@ a.uglyfirstahack {
}

body#popup {
min-width: 400px;
min-width: 420px;
}
2 changes: 1 addition & 1 deletion icinga.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const HSTATE_PEND = 1;
const HSTATE_UNR = 4;
const HSTATE_DOWN = 8;

var debug = false;
var debug = true;

function debug_log(msg)
{
Expand Down
Binary file added img/icon_38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f0ed4a7

Please sign in to comment.