From 56081d37f368e69c95c8e9e8635623e915dae8f1 Mon Sep 17 00:00:00 2001 From: martijn Date: Tue, 24 Oct 2023 18:16:05 +0000 Subject: [PATCH] Add support for HOST-RESOURCES-MIB:hrSWRunPerfTable by adding hrSWRunPerfCPU and hrSWRunPerfMem. This allows snmptop to work with snmpd(8). Math copied from top(1). OK tb@ --- libexec/snmpd/snmpd_metrics/mib.c | 36 ++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libexec/snmpd/snmpd_metrics/mib.c b/libexec/snmpd/snmpd_metrics/mib.c index 0846307f5d9..8c3e8eb32bb 100644 --- a/libexec/snmpd/snmpd_metrics/mib.c +++ b/libexec/snmpd/snmpd_metrics/mib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mib.c,v 1.4 2023/07/04 11:34:19 sashan Exp $ */ +/* $OpenBSD: mib.c,v 1.5 2023/10/24 18:16:05 martijn Exp $ */ /* * Copyright (c) 2022 Martijn van Duren @@ -69,6 +69,10 @@ struct event connev; const char *agentxsocket = NULL; int agentxfd = -1; +int pageshift; +#define pagetok(size) ((size) << pageshift) + +void pageshift_init(void); void snmp_connect(struct agentx *, void *, int); void snmp_tryconnect(int, short, void *); void snmp_read(int, short, void *); @@ -89,6 +93,7 @@ struct agentx_object *hrProcessorLoad; struct agentx_index *hrSWRunIdx; struct agentx_object *hrSWRunIndex, *hrSWRunName, *hrSWRunID, *hrSWRunPath; struct agentx_object *hrSWRunParameters, *hrSWRunType, *hrSWRunStatus; +struct agentx_object *hrSWRunPerfCPU, *hrSWRunPerfMem; void mib_hrsystemuptime(struct agentx_varbind *); void mib_hrsystemdate(struct agentx_varbind *); @@ -634,6 +639,7 @@ mib_hrswrun(struct agentx_varbind *vb) struct agentx_object *obj; enum agentx_request_type req; int32_t idx; + int32_t time; struct kinfo_proc *kinfo; char *s; @@ -711,6 +717,13 @@ mib_hrswrun(struct agentx_varbind *vb) agentx_varbind_integer(vb, 4); break; } + } else if (obj == hrSWRunPerfCPU) { + time = kinfo->p_rtime_sec * 100; + time += (kinfo->p_rtime_usec + 5000) / 10000; + agentx_varbind_integer(vb, time); + } else if (obj == hrSWRunPerfMem) { + agentx_varbind_integer(vb, pagetok(kinfo->p_vm_tsize + + kinfo->p_vm_dsize + kinfo->p_vm_ssize)); } else fatal("%s: Unexpected object", __func__); } @@ -3278,6 +3291,7 @@ main(int argc, char *argv[]) kr_init(); pf_init(); timer_init(); + pageshift_init(); if (agentxsocket != NULL) { if (strlcpy(agentxsocketdir, agentxsocket, @@ -3375,6 +3389,10 @@ main(int argc, char *argv[]) (hrSWRunType = agentx_object(host, AGENTX_OID(HRSWRUNTYPE), &hrSWRunIdx, 1, 0, mib_hrswrun)) == NULL || (hrSWRunStatus = agentx_object(host, AGENTX_OID(HRSWRUNSTATUS), + &hrSWRunIdx, 1, 0, mib_hrswrun)) == NULL || + (hrSWRunPerfCPU = agentx_object(host, AGENTX_OID(HRSWRUNPERFCPU), + &hrSWRunIdx, 1, 0, mib_hrswrun)) == NULL || + (hrSWRunPerfMem = agentx_object(host, AGENTX_OID(HRSWRUNPERFMEM), &hrSWRunIdx, 1, 0, mib_hrswrun)) == NULL) fatal("agentx_object"); @@ -4320,6 +4338,22 @@ main(int argc, char *argv[]) event_dispatch(); } +#define LOG1024 10 +void +pageshift_init(void) +{ + long pagesize; + + if ((pagesize = sysconf(_SC_PAGESIZE)) == -1) + fatal("sysconf(_SC_PAGESIZE)"); + while (pagesize > 1) { + pageshift++; + pagesize >>= 1; + } + /* we only need the amount of log(2)1024 for our conversion */ + pageshift -= LOG1024; +} + void snmp_connect(struct agentx *sa, void *cookie, int close) { -- 2.20.1