parent
9b0c0af8b8
commit
b4f700c185
@ -0,0 +1,61 @@
|
|||||||
|
/* eslint no-underscore-dangle: ["error", { "allow": ["_text", "_cdata"] }] */
|
||||||
|
|
||||||
|
import { useTranslation } from "next-i18next";
|
||||||
|
|
||||||
|
import Container from "components/services/widget/container";
|
||||||
|
import Block from "components/services/widget/block";
|
||||||
|
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||||
|
|
||||||
|
export default function Component({ service }) {
|
||||||
|
const { t } = useTranslation("common");
|
||||||
|
|
||||||
|
const { widget } = service;
|
||||||
|
|
||||||
|
const { data: statusData, error: statusError } = useWidgetAPI(widget, "status");
|
||||||
|
|
||||||
|
if (statusError) {
|
||||||
|
return <Container service={service} error={statusError} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!statusData) {
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="qnap.cpuUsage" />
|
||||||
|
<Block label="qnap.memUsage" />
|
||||||
|
<Block label="qnap.systemTempC" />
|
||||||
|
<Block label="qnap.poolUsage" />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cpuUsage = statusData.system.cpu_usage._cdata.replace(' %','');
|
||||||
|
const totalMemory = statusData.system.total_memory._cdata;
|
||||||
|
const freeMemory = statusData.system.free_memory._cdata;
|
||||||
|
const systemTempC = statusData.system.cpu_tempc._text;
|
||||||
|
|
||||||
|
const volumeTotalSize = statusData.volume.volumeUse.total_size._cdata;
|
||||||
|
const volumeFreeSize = statusData.volume.volumeUse.free_size._cdata;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block
|
||||||
|
label="qnap.cpuUsage"
|
||||||
|
value={t("common.percent", { value: (cpuUsage) })}
|
||||||
|
/>
|
||||||
|
<Block
|
||||||
|
label="qnap.memUsage"
|
||||||
|
value={t("common.percent", { value: (((totalMemory - freeMemory) / totalMemory) * 100).toFixed(0) })}
|
||||||
|
/>
|
||||||
|
<Block
|
||||||
|
label="qnap.systemTempC"
|
||||||
|
value={`${systemTempC} °C`}
|
||||||
|
/>
|
||||||
|
<Block
|
||||||
|
label="qnap.poolUsage"
|
||||||
|
value={t("common.percent", { value: (((volumeTotalSize - volumeFreeSize) / volumeTotalSize) * 100).toFixed(0) })}
|
||||||
|
/>
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
/* eslint no-underscore-dangle: ["error", { "allow": ["_text", "_cdata"] }] */
|
||||||
|
|
||||||
|
import cache from "memory-cache";
|
||||||
|
import { xml2json } from "xml-js";
|
||||||
|
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
|
||||||
|
const proxyName = "qnapProxyHandler";
|
||||||
|
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
|
||||||
|
const logger = createLogger(proxyName);
|
||||||
|
|
||||||
|
async function login(widget, service) {
|
||||||
|
const endpoint = "{url}/cgi-bin/authLogin.cgi";
|
||||||
|
const loginUrl = new URL(formatApiCall(endpoint, widget ));
|
||||||
|
const headers = { "Content-Type": "application/x-www-form-urlencoded" };
|
||||||
|
|
||||||
|
const [, , data,] = await httpProxy(loginUrl, {
|
||||||
|
method: "POST",
|
||||||
|
body: new URLSearchParams({
|
||||||
|
user: widget.username,
|
||||||
|
pwd: Buffer.from(`${widget.password}`).toString("base64")
|
||||||
|
}).toString(),
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dataDecoded = xml2json(data.toString(), { compact: true });
|
||||||
|
const jsonData = JSON.parse(dataDecoded);
|
||||||
|
const token = jsonData.QDocRoot.authSid._cdata;
|
||||||
|
cache.put(`${sessionTokenCacheKey}.${service}`, token);
|
||||||
|
return { token };
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Unable to login to QNAP API: %s", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { token: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function apiCall(widget, endpoint, service) {
|
||||||
|
let key = cache.get(`${sessionTokenCacheKey}.${service}`);
|
||||||
|
const method = "GET";
|
||||||
|
|
||||||
|
let apiUrl = new URL(formatApiCall(`${endpoint}&sid=${key}`, widget));
|
||||||
|
let [status, contentType, data, responseHeaders] = await httpProxy(apiUrl, {
|
||||||
|
method
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status === 404) {
|
||||||
|
logger.error("QNAP API rejected the request, attempting to obtain new session token");
|
||||||
|
key = await login(widget, service);
|
||||||
|
apiUrl = new URL(formatApiCall(`${endpoint}&sid=${key}`, widget));
|
||||||
|
[status, contentType, data, responseHeaders] = await httpProxy(apiUrl, {
|
||||||
|
method
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.error("Error getting data from QNAP: %s status %d. Data: %s", apiUrl, status, data);
|
||||||
|
return { status, contentType, data: null, responseHeaders };
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataDecoded = xml2json(data.toString(), { compact: true });
|
||||||
|
logger.debug("Dayta '%s'", dataDecoded);
|
||||||
|
return { status, contentType, data: JSON.parse(dataDecoded.toString()), responseHeaders };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function qnapProxyHandler(req, res) {
|
||||||
|
const { group, service } = req.query;
|
||||||
|
|
||||||
|
if (!group || !service) {
|
||||||
|
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
if (!widget) {
|
||||||
|
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cache.get(`${sessionTokenCacheKey}.${service}`)) {
|
||||||
|
await login(widget, service);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: systemStatsData } = await apiCall(widget, "{url}/cgi-bin/management/manaRequest.cgi?subfunc=sysinfo&hd=no&multicpu=1", service);
|
||||||
|
const { data: volumeStatsData } = await apiCall(widget, "{url}/cgi-bin/management/chartReq.cgi?chart_func=disk_usage&disk_select=all&include=all", service);
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
system: systemStatsData.QDocRoot.func.ownContent.root,
|
||||||
|
volume: volumeStatsData.QDocRoot.volumeUseList
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
import qnapProxyHandler from "./proxy";
|
||||||
|
|
||||||
|
const widget = {
|
||||||
|
api: "{url}",
|
||||||
|
proxyHandler: qnapProxyHandler,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default widget;
|
Loading…
Reference in new issue