Skip to content

Instantly share code, notes, and snippets.

@peltho
Created July 12, 2016 12:21
Show Gist options
  • Save peltho/e38098646740fc9ce51d2880babdf426 to your computer and use it in GitHub Desktop.
Save peltho/e38098646740fc9ce51d2880babdf426 to your computer and use it in GitHub Desktop.
CasperJS Extension UI to Zabbix
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'modules/build/ZabbixApi.class.php';
use ZabbixApi\ZabbixApi;
try
{
$dir = "/appli/casperjs_scripts";
$integ = scandir($dir."/integ");
$integDir = $dir."/integ";
$prod = scandir($dir."/prod");
$prodDir = $dir."/prod";
for($i=0; $i<2; $i++) {
unset($integ[$i]);
unset($prod[$i]);
}
$integ = array_values($integ);
$prod = array_values($prod);
// connect to Zabbix API
$api = new ZabbixApi('http://rsl4154:8080/zabbix/api_jsonrpc.php', 'admin', '1d38dfcca23222911787a177ec58534');
$scripts = $api->itemGet(array(
'search' => array('name' => 'casper')
));
if (isset($_POST['status']) && isset($_POST['itemid'])) {
$status = htmlentities($_POST['status']);
$id = htmlentities($_POST['itemid']);
$api->itemUpdate(array('itemid' => $id, 'status' => $status));
}
if (isset($_POST['status']) && isset($_POST['file']) && isset($_POST['env'])) {
$status = htmlentities($_POST['status']);
$file = htmlentities($_POST['file']);
$env = htmlentities($_POST['env']);
if(file_exists($env.'/'.$file)) {
if($status == 0) {
$newfilename = preg_replace('/\.disabled/', '', $file);
rename($env.'/'.$file, $env.'/'.$newfilename);
} else {
if(preg_match('/\.disabled/', $file) != 1) {
rename($env.'/'.$file, $env.'/'.$file.'.disabled');
}
}
} else {
echo "Error: This scenario doesn't exists !";
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Zabbix - CasperJS module</title>
<meta charset="utf-8"/>
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="styles/blue-theme.css">
</head>
<body>
<header role="banner">
<div class="nav" role="navigation">
<div class="top-nav-container" id="mmenu">
<a href="zabbix.php?action=dashboard.view"><div class="logo"></div></a>
</div>
<div class="top-subnav-container">
<ul class="top-subnav">
<li><a href="zabbix.php?action=dashboard.view">Back</a></li>
</ul>
</div>
</div>
</header>
<div class="article">
<div class="header-title">
<h1>CasperJS Scripts</h1>
<table class="list-table">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<?php foreach($scripts as $s) { ?>
<tr>
<td class="nowrap">
<?php
echo '<table><tr><td><b>'.$s->name.'</b></td></tr>';
if(preg_match('/Integ/', $s->name) == 1) {
foreach($integ as $i) {
$file = $i;
$i = preg_replace('/\.js/', '', $i);
?>
<tr>
<td><?= ucwords(preg_replace('/_/', ' ', preg_replace('/\.disabled/', '', $i))); ?></td>
<td class="disable" data-itemid="<?= $s->itemid; ?>" <?php if ($s->status == 1) {?>style="display:none;"<?php }?>>
<?php if(preg_match('/\.disabled$/', $i)) {?>
<a class="script link-action red" data-status="0" data-file="<?= $file ?>" data-env="<?= $integDir ?>">Disabled</a>
<?php } else {?>
<a class="script link-action green" data-status="1" data-file="<?= $file ?>" data-env="<?= $integDir ?>">Enabled</a>
<?php } ?>
</td>
</tr>
<?php
}
}
if(preg_match('/Prod/', $s->name) == 1) {
foreach($prod as $p) {
$file = $p;
$p = preg_replace('/\.js/', '', $p);
?>
<tr>
<td><?= ucwords(preg_replace('/_/', ' ', preg_replace('/\.disabled/', '', $p))); ?></td>
<td class="disable" data-itemid="<?= $s->itemid; ?>" <?php if ($s->status == 1) {?>style="display:none;"<?php }?>>
<?php if(preg_match('/\.disabled$/', $p)) {?>
<a class="script link-action red" data-status="0" data-file="<?= $file ?>" data-env="<?= $prodDir ?>">Disabled</a>
<?php } else {?>
<a class="script link-action green" data-status="1" data-file="<?= $file ?>" data-env="<?= $prodDir ?>">Enabled</a>
<?php } ?>
</td>
</tr>
<?php
}
}
echo '</table>';
?>
</td>
<td>
<a data-itemid="<?= $s->itemid; ?>" data-status="<?= $s->status; ?>" <?php if($s->status == 0) {?>class="env link-action green"<?php } else { ?>class="env link-action red"<?php } ?>><?php if($s->status == 0) { ?>Enabled<?php } else { ?>Disabled<?php } ?></a>
</td>
<td>"<?= $s->key_; ?>"</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("a.env").click(function(){
var id = $(this).data('itemid');
var status = $(this).data('status');
status = (status == 0) ? 1 : 0;
$.ajax({
url: 'casperjs.php',
type: 'POST',
data: {
status: status,
itemid: id
},
success: function(res) {}
});
if (status == 0) {
$(this).removeClass('env link-action red').addClass('env link-action green');
$(this).text('Enabled');
$(this).attr('data-status', 0);
$(this).removeData('status');
$('td[data-itemid="'+id+'"][class="disable"]').show();
} else {
$(this).removeClass('env link-action green').addClass('env link-action red');
$(this).text('Disabled');
$(this).attr('data-status', 1);
$(this).removeData('status');
$('td[data-itemid="'+id+'"][class="disable"]').hide();
}
});
$("a.script").click(function(){
var status = $(this).data('status');
var file = $(this).data('file');
var env = $(this).data('env');
$.ajax({
url: 'casperjs.php',
type: 'POST',
data: {
status: status,
file: file,
env: env
},
success: function(res) {}
});
if (status == 0) {
$(this).removeClass('script link-action red').addClass('script link-action green');
$(this).text('Enabled');
$(this).removeData('status');
$(this).attr('data-status', 1);
$(this).removeData('file');
$(this).attr('data-file', file.replace('.disabled', ''));
} else {
$(this).removeClass('script link-action green').addClass('script link-action red');
$(this).text('Disabled');
$(this).removeData('status');
$(this).attr('data-status', 0);
$(this).removeData('file');
$(this).attr('data-file', file+".disabled");
// $(this).data('file', file+".disabled");
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment