dec-hw: dual power supply status for Deciso appliances (#3687)

- API endpoint to query the current power supply status
- small dashboard widget
This commit is contained in:
Stephan de Wit
2023-11-29 09:54:20 +01:00
committed by GitHub
parent 0e0ff854db
commit 0b70e35c8c
9 changed files with 200 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/usr/local/etc/rc.syshook.d/early/30-powerstat
+8
View File
@@ -0,0 +1,8 @@
PLUGIN_NAME= dec-hw
PLUGIN_VERSION= 1.0
PLUGIN_COMMENT= Deciso Hardware specific information
PLUGIN_MAINTAINER= stephan.de.wit@deciso.com
PLUGIN_TIER= 2
.include "../../Mk/plugins.mk"
+4
View File
@@ -0,0 +1,4 @@
This package allows fetching the current power status for Deciso
appliances with dual power supplies via an API call and includes a simple
dashboard widget.
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
AMDGPIO="/boot/kernel/amdgpio.ko"
if [ ! -e "$AMDGPIO" ]; then
echo "Error: amdpgio kernel module missing"
logger -t "dec-hw" "Error: amdpgio kernel module missing"
exit 1
fi
kldload "$AMDGPIO" 2>&1
exit 0
@@ -0,0 +1,57 @@
<?php
/*
* Copyright (C) 2023 Deciso B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace OPNsense\dechw\Api;
use OPNsense\Base\ApiControllerBase;
use OPNsense\Core\Backend;
class InfoController extends ApiControllerBase
{
public function powerStatusAction()
{
$result = [
"status" => "failed",
"status_translated" => gettext("Power status could not be fetched.
This widget is only applicable to Deciso hardware with dual power supplies.")
];
$status = parse_ini_string((new Backend())->configdRun('dechw power'));
if (!empty($status)) {
$result["status"] = "OK";
unset($result["status_translated"]);
foreach (['pwr1', 'pwr2'] as $key) {
$result[$key . '_translated'] = $status[$key] === '1' ? gettext('On') : gettext('Off');
}
$result = array_merge($result, $status);
}
return $result;
}
}
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
GPIOC="/dev/gpioc0"
if [ ! -e "$GPIOC" ]; then
logger -t "dec-hw" "Error: GPIO device does not exist, exiting."
exit 0
fi
i=1
for PIN in 4 5; do
STATUS=$(gpioctl -f "$GPIOC" "$PIN")
printf "pwr%d=%d\n" "$i" "$STATUS"
i=$((i + 1))
done
@@ -0,0 +1,5 @@
[power]
command:/usr/local/opnsense/scripts/dec-hw/powerstat
parameters:
type:script_output
message:Deciso PWR LED state
@@ -0,0 +1,3 @@
<?php
$dechw_title = gettext('Deciso Hardware Information');
@@ -0,0 +1,94 @@
<?php
/**
* Copyright (C) 2023 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
?>
<script>
$(document).ready(function() {
ajaxGet('/api/dechw/info/powerStatus', {}, function(data, status) {
if (data['status'] === 'failed') {
$('#status').html(data['status_translated']);
$('.pwr-container').hide();
return;
}
['pwr1', 'pwr2'].forEach(function(key) {
let status = data[key];
let status_translated = data[key + '_translated'];
let $power = $('<span data-toggle="tooltip" title=""></span>').addClass('power fa fa-power-off fa-lg');
$power.css('color', status === '1' ? 'blue' : 'red');
$power.attr('title', status_translated);
$('#'+key).append($power);
})
$(".circle").tooltip({container: 'body', trigger: 'hover'});
});
});
</script>
<style>
#status {
margin: 10px;
}
.power {
margin: 5px;
float: right;
}
.power:hover {
opacity: 0.5;
}
.pwr-container {
margin: 5px;
display: flex;
justify-content: center;
align-items: center;
}
.data-item {
padding: 10px;
border: 1px solid #ddd;
margin: 5px;
width: 50%;
display: inline-block;
}
</style>
<div id="status"></div>
<div class="pwr-container">
<div id="pwr1" class="data-item">
<strong><?=gettext("Power Supply 1");?></strong>
</div>
<div id="pwr2" class="data-item">
<strong><?=gettext("Power Supply 2");?></strong>
</div>
</div>