Skip to content

Instantly share code, notes, and snippets.

@itskenny0
Created October 31, 2016 15:42
Show Gist options
  • Save itskenny0/2749a876a912649acac2c7fd5994d30a to your computer and use it in GitHub Desktop.
Save itskenny0/2749a876a912649acac2c7fd5994d30a to your computer and use it in GitHub Desktop.
<?php
class CheckMKLocalcheck {
private $text = "Value returned: :value:";
private $metrics = array();
private $name;
private $value;
private $kpis = array(
self::STATE_WARN => "1",
self::STATE_CRIT => "2"
);
const STATE_WARN = 1;
const STATE_CRIT = 2;
const OUTPUT_TEMPLATE = ":state: :name::metrics: :text:";
public function __construct($name) {
$this->name = $name;
}
public function setText($value) {
if(!is_string($value)) throw new Exception("This function only accepts strings.");
$this->text = $value;
return $this;
}
public function setValue($value) {
if(!is_integer($value)) throw new Exception("$value needs to be numerical.");
$this->value = $value;
return $this;
}
public function setAllKPI($ok, $warn, $crit) {
if(!is_integer($ok) || !is_integer($warn) || !is_integer($crit)) throw new Exception("Values need to be numerical.");
$self->setKPI(CheckMKLocalcheck::STATE_WARN, "300");
$self->setKPI(CheckMKLocalcheck::STATE_CRIT, "500");
return $this;
}
public function setKPI($kpi, $value) {
if(!is_integer($value)) throw new Exception("$value needs to be numerical.");
$this->kpis[$kpi] = $value;
return $this;
}
public function setMetric($name, $value) {
if(!is_integer($value)) throw new Exception("$value needs to be numerical.");
$this->metrics[$name] = $value;
return $this;
}
public function export() {
if(empty($this->value)) throw new Exception("Attempted to export without setting value.");
if(empty($this->name)) throw new Exception("Attempted to export without setting name.");
// Calculate state value
switch(true) {
case $this->value >= $this->kpis[self::STATE_CRIT]:
$state = 2;
break;
case $this->value >= $this->kpis[self::STATE_WARN]:
$state = 1;
break;
default:
$state = 0;
break;
}
$metrics = "";
foreach($this->metrics as $metric => $value) {
$metrics .= " " . $metric . "=" . $value;
}
$text = str_replace(":value:", $this->value, $this->text);
$outputText = self::OUTPUT_TEMPLATE;
$outputText = str_replace(":state:", $state, $outputText);
$outputText = str_replace(":name:", $this->name, $outputText);
$outputText = str_replace(":text:", $text, $outputText);
$outputText = str_replace(":metrics:", $metrics, $outputText);
return $outputText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment