parent
c18fd02c8e
commit
6bcaec8003
@ -0,0 +1,20 @@
|
||||
---
|
||||
title: Ceph
|
||||
description: Ceph Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Ceph API](https://docs.ceph.com/en/latest/api/).
|
||||
|
||||
The username and password are the same as used to login to the web interface.
|
||||
|
||||
Allowed fields: `["status", "alerts", "freespace", "usedspace", "free", "used", "read", "write", "recovering"]`.
|
||||
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: ceph
|
||||
url: http://ceph.host.or.ip:port
|
||||
username: user1
|
||||
password: password1
|
||||
fields: ["status", "alerts", "used"]
|
||||
```
|
@ -0,0 +1,68 @@
|
||||
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();
|
||||
const { widget } = service;
|
||||
|
||||
const { data: infoData, error: infoError } = useWidgetAPI(widget, "ceph/proxy-hosts");
|
||||
|
||||
if (infoError) {
|
||||
return <Container service={service} error={infoError} />;
|
||||
}
|
||||
|
||||
// Provide a default if not set in the config
|
||||
if (!widget.fields) {
|
||||
widget.fields = ["status", "alerts", "used"];
|
||||
}
|
||||
|
||||
// Limit to a maximum of 4 at a time
|
||||
if (widget.fields.length > 4) {
|
||||
widget.fields = widget.fields.slice(0, 4);
|
||||
}
|
||||
/*
|
||||
"status": "Status",
|
||||
"alerts": "Alerts",
|
||||
"freespace": "Free Space",
|
||||
"usedspace": "Used Space",
|
||||
"free": "Free",
|
||||
"used": "Used",
|
||||
"read": "Read",
|
||||
"write": "Write",
|
||||
"recovering": "Recovering"
|
||||
|
||||
*/
|
||||
|
||||
if (!infoData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="ceph.status" />
|
||||
<Block label="ceph.alerts" />
|
||||
<Block label="ceph.freespace" />
|
||||
<Block label="ceph.usedspace" />
|
||||
<Block label="ceph.free" />
|
||||
<Block label="ceph.used" />
|
||||
<Block label="ceph.read" />
|
||||
<Block label="ceph.write" />
|
||||
<Block label="ceph.recovering" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="ceph.status" value={infoData.health.status} />
|
||||
<Block label="ceph.alerts" value={infoData.health.checks?.length} />
|
||||
<Block label="ceph.freespace" value={t("common.bbytes", { value: infoData.df.stats.total_avail_bytes, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.usedspace" value={t("common.bbytes", { value: infoData.df.stats.total_used_bytes, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.free" value={t("common.percent", { value: (infoData.df.stats.total_avail_bytes / infoData.df.stats.total_bytes) * 100, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.used" value={t("common.percent", { value: (infoData.df.stats.total_used_bytes / infoData.df.stats.total_bytes) * 100, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.read" value={t("common.byterate", { value: infoData.client_perf.read_bytes_sec, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.write" value={t("common.byterate", { value: infoData.client_perf.write_bytes_sec, maximumFractionDigits: 1 })} />
|
||||
<Block label="ceph.recovering" value={t("common.byterate", { value: infoData.client_perf.recovering_bytes_per_sec, maximumFractionDigits: 1 })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
import cache from "memory-cache";
|
||||
|
||||
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 = "cephProxyHandler";
|
||||
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
async function login(widget) {
|
||||
|
||||
const loginUrl = new URL(formatApiCall("{url}/api/auth", widget));
|
||||
|
||||
const [status, , data] = await httpProxy(loginUrl, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username: widget.username, password: widget.password }),
|
||||
headers: {
|
||||
"accept": "application/vnd.ceph.api.v1.0+json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
// try to avoid parsing errors that are not from ceph
|
||||
if (status >= 500)
|
||||
{
|
||||
logger.error("Failed to connect to %s, status: %d, detail: %s", loginUrl, status, data?.error?.message ?? "-- Unable to read error message from request");
|
||||
return [status, false ];
|
||||
}
|
||||
const dataParsed = JSON.parse(data);
|
||||
|
||||
if (!(status === 201) || !dataParsed.token) {
|
||||
logger.error("Failed to login to Ceph, status: %d, detail: %s", status, dataParsed?.detail);
|
||||
return [status, false ];
|
||||
}
|
||||
|
||||
return [ status, dataParsed.token ];
|
||||
}
|
||||
|
||||
export default async function cephProxyHandler(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 (!widget.url) {
|
||||
return res.status(500).json({ error: { message: "Service widget url not configured" } });
|
||||
}
|
||||
|
||||
let token = cache.get(`${sessionTokenCacheKey}.${service}`);
|
||||
|
||||
const url = new URL(formatApiCall("{url}/api/health/full", widget));
|
||||
const params = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"accept": "application/vnd.ceph.api.v1.0+json",
|
||||
"Authorization": `Bearer ${token}`
|
||||
}
|
||||
};
|
||||
|
||||
let [status, , data] = await httpProxy(url, params);
|
||||
|
||||
if (status === 401) {
|
||||
[status, token] = await login(widget);
|
||||
|
||||
if (status !== 201) {
|
||||
logger.error("HTTP %d logging in to ceph", status);
|
||||
return res.status(status).end(data);
|
||||
}
|
||||
|
||||
cache.put(`${sessionTokenCacheKey}.${service}`, token);
|
||||
params.headers.Authorization = `Bearer ${token}`;
|
||||
[status, , data] = await httpProxy(url, params);
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d getting data from ceph. Data: %s", status, data);
|
||||
}
|
||||
|
||||
return res.status(status).send(data);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
import cephProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
proxyHandler: cephProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue