sync code with last improvements from OpenBSD

This commit is contained in:
purplerain 2023-11-11 01:29:48 +00:00
parent 5903cbe575
commit 62d64fa864
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
841 changed files with 83929 additions and 40755 deletions

View file

@ -1,4 +1,4 @@
/* $OpenBSD: mib.c,v 1.4 2023/07/04 11:34:19 sashan Exp $ */
/* $OpenBSD: mib.c,v 1.6 2023/10/24 18:27:26 martijn Exp $ */
/*
* Copyright (c) 2022 Martijn van Duren <martijn@openbsd.org>
@ -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 *);
@ -103,7 +108,9 @@ int kinfo_proc_comp(const void *, const void *);
int kinfo_proc(u_int32_t, struct kinfo_proc **);
void kinfo_timer_cb(int, short, void *);
void kinfo_proc_free(void);
int kinfo_args(struct kinfo_proc *, char **);
int kinfo_args(struct kinfo_proc *, char ***);
int kinfo_path(struct kinfo_proc *, char **);
int kinfo_parameters(struct kinfo_proc *, char **);
/* IF-MIB */
struct agentx_index *ifIdx;
@ -634,6 +641,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;
@ -669,13 +677,21 @@ mib_hrswrun(struct agentx_varbind *vb)
if (obj == hrSWRunIndex)
agentx_varbind_integer(vb, kinfo->p_pid);
else if (obj == hrSWRunName || obj == hrSWRunPath)
else if (obj == hrSWRunName)
agentx_varbind_string(vb, kinfo->p_comm);
else if (obj == hrSWRunID)
else if (obj == hrSWRunPath) {
if (kinfo_path(kinfo, &s) == -1) {
log_warn("kinfo_path");
agentx_varbind_error(vb);
return;
}
agentx_varbind_string(vb, s);
} else if (obj == hrSWRunID)
agentx_varbind_oid(vb, AGENTX_OID(0, 0));
else if (obj == hrSWRunParameters) {
if (kinfo_args(kinfo, &s) == -1) {
log_warn("kinfo_args");
if (kinfo_parameters(kinfo, &s) == -1) {
log_warn("kinfo_parameters");
agentx_varbind_error(vb);
return;
}
@ -711,6 +727,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__);
}
@ -811,25 +834,22 @@ kinfo_proc_free(void)
}
int
kinfo_args(struct kinfo_proc *kinfo, char **s)
kinfo_args(struct kinfo_proc *kinfo, char ***s)
{
static char str[128];
static char *buf = NULL;
static size_t buflen = 128;
int mib[] = { CTL_KERN, KERN_PROC_ARGS,
kinfo->p_pid, KERN_PROC_ARGV };
char *nbuf, **argv;
char *nbuf;
*s = NULL;
if (buf == NULL) {
buf = malloc(buflen);
if (buf == NULL)
return (-1);
}
str[0] = '\0';
*s = str;
while (sysctl(mib, nitems(mib), buf, &buflen, NULL, 0) == -1) {
if (errno != ENOMEM) {
/* some errors are expected, dont get too upset */
@ -844,11 +864,41 @@ kinfo_args(struct kinfo_proc *kinfo, char **s)
buflen += 128;
}
argv = (char **)buf;
if (argv[0] == NULL)
return (0);
*s = (char **)buf;
return (0);
}
int
kinfo_path(struct kinfo_proc *kinfo, char **s)
{
static char str[129];
char **argv;
if (kinfo_args(kinfo, &argv) == -1)
return (-1);
str[0] = '\0';
*s = str;
if (argv != NULL && argv[0] != NULL)
strlcpy(str, argv[0], sizeof(str));
return (0);
}
int
kinfo_parameters(struct kinfo_proc *kinfo, char **s)
{
static char str[129];
char **argv;
if (kinfo_args(kinfo, &argv) == -1)
return (-1);
str[0] = '\0';
*s = str;
if (argv == NULL || argv[0] == NULL)
return (0);
argv++;
while (*argv != NULL) {
strlcat(str, *argv, sizeof(str));
argv++;
@ -3278,6 +3328,7 @@ main(int argc, char *argv[])
kr_init();
pf_init();
timer_init();
pageshift_init();
if (agentxsocket != NULL) {
if (strlcpy(agentxsocketdir, agentxsocket,
@ -3375,6 +3426,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 +4375,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)
{