portfolio/lib/grafana.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-02-09 22:30:35 +00:00
import fetcher from '@/lib/fetcher'
const GRAFANA_URL = process.env.GRAFANA_URL
const GRAFANA_TOKEN = process.env.GRAFANA_TOKEN
2023-02-10 17:02:15 +00:00
export const getTemp = async () => {
2023-02-09 22:30:35 +00:00
const response = await fetcher(GRAFANA_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${GRAFANA_TOKEN}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"queries": [
{
"datasource": {
"uid": "4f-R6jgRz",
"type": "prometheus"
},
"expr": "node_hwmon_temp_celsius",
2023-02-10 17:02:15 +00:00
"maxDataPoints": 100
2023-02-09 22:30:35 +00:00
}
],
"from": "now-5m",
"to": "now"
})
})
return {
temp: response.results.A.frames[0].data.values[1][15].toFixed(2)
}
2023-02-10 17:02:15 +00:00
}
export const getRootFsUsage = async () => {
const response = await fetcher(GRAFANA_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${GRAFANA_TOKEN}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"queries": [
{
"datasource": {
"uid": "4f-R6jgRz",
"type": "prometheus"
},
"expr": "100 - ((node_filesystem_avail_bytes{mountpoint='/',fstype!='rootfs'} * 100) / node_filesystem_size_bytes{mountpoint='/',fstype!='rootfs'})",
"maxDataPoints": 100
}
],
"from": "now-5m",
"to": "now"
})
})
return {
usage: response.results.A.frames[0].data.values[1][15].toFixed(2)
}
}
export const getUptime = async () => {
const response = await fetcher(GRAFANA_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${GRAFANA_TOKEN}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"queries": [
{
"datasource": {
"uid": "4f-R6jgRz",
"type": "prometheus"
},
"expr": "node_time_seconds - node_boot_time_seconds",
"maxDataPoints": 100
}
],
"from": "now-5m",
"to": "now"
})
})
const seconds = response.results.A.frames[0].data.values[1][15]
const minutes = (seconds / 60).toFixed(2)
const hours = (seconds / 3_600).toFixed(2)
const weeks = (seconds / 604_800).toFixed(2)
return {
seconds: (seconds).toFixed(2),
minutes,
hours,
weeks
}
2023-02-09 22:30:35 +00:00
}