portfolio/lib/pi.ts

171 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-02-09 22:30:35 +00:00
import fetcher from '@/lib/fetcher'
2023-02-22 22:17:24 +00:00
const GRAFANA_URL: string = process.env.GRAFANA_URL ?? ""
2023-02-09 22:30:35 +00:00
const GRAFANA_TOKEN = process.env.GRAFANA_TOKEN
2023-07-31 12:37:17 +00:00
const day = 24 * 60 * 60 * 1000;
const yesterday = Date.now() - day;
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
}
],
2023-07-31 12:37:17 +00:00
"from": yesterday.toString(),
2023-02-09 22:30:35 +00:00
"to": "now"
})
})
2023-04-15 21:06:56 +00:00
const temp = parseInt(response.results.A.frames[0].data.values[1].slice(-1))
2023-02-09 22:30:35 +00:00
return {
2023-04-15 21:06:56 +00:00
temp
2023-02-09 22:30:35 +00:00
}
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
}
],
2023-07-31 12:37:17 +00:00
"from": yesterday.toString(),
2023-02-10 17:02:15 +00:00
"to": "now"
})
})
2023-04-15 21:06:56 +00:00
const rootFsUsage = parseInt(response.results.A.frames[0].data.values[1].slice(-1))
2023-02-10 17:02:15 +00:00
return {
2023-04-15 21:06:56 +00:00
rootFsUsage
2023-02-10 17:02:15 +00:00
}
}
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
}
],
2023-07-31 12:37:17 +00:00
"from": yesterday.toString(),
2023-02-10 17:02:15 +00:00
"to": "now"
})
})
2023-04-15 21:06:56 +00:00
const seconds = parseInt(response.results.A.frames[0].data.values[1].slice(-1))
const minutes = (seconds / 60)
const hours = (seconds / 3_600)
const days = (seconds / 86400)
const weeks = (seconds / 604_800)
2023-02-10 17:02:15 +00:00
return {
2023-04-15 21:06:56 +00:00
seconds: (seconds),
2023-02-10 17:02:15 +00:00
minutes,
hours,
2023-02-10 21:52:00 +00:00
days,
2023-02-10 17:02:15 +00:00
weeks
}
2023-02-10 21:52:00 +00:00
}
export const getRamUsage = 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_memory_MemAvailable_bytes * 100) / node_memory_MemTotal_bytes)",
"maxDataPoints": 100
}
],
2023-07-31 12:37:17 +00:00
"from": yesterday.toString(),
2023-02-10 21:52:00 +00:00
"to": "now"
})
})
2023-04-15 21:06:56 +00:00
const ramUsage = parseInt(response.results.A.frames[0].data.values[1].slice(-1))
2023-02-10 21:52:00 +00:00
return {
2023-04-15 21:06:56 +00:00
ramUsage
2023-02-10 21:52:00 +00:00
}
}
export const getSysLoad = 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": "avg(node_load5) / count(count(node_cpu_seconds_total) by (cpu)) * 100",
"maxDataPoints": 100
}
],
2023-07-31 12:37:17 +00:00
"from": yesterday.toString(),
2023-02-10 21:52:00 +00:00
"to": "now"
})
})
2023-04-15 21:06:56 +00:00
const sysLoad = parseInt(response.results.A.frames[0].data.values[1].slice(-1))
2023-02-10 21:52:00 +00:00
return {
2023-04-15 21:06:56 +00:00
sysLoad
2023-02-10 21:52:00 +00:00
}
2023-07-31 12:37:17 +00:00
}