1/*
2 * Copyright (c) 2005-2006 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
6 *
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
--- 12 unchanged lines hidden (view full) ---
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c 154860 2006-01-26 12:19:10Z harti $
30 *
31 * Host Resources MIB for SNMPd. Implementation for hrSWRunTable
32 */
33
34#include <sys/param.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37#include <sys/user.h>
--- 4 unchanged lines hidden (view full) ---
42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45
46#include "hostres_snmp.h"
47#include "hostres_oid.h"
48#include "hostres_tree.h"
49
50
51/*
52 * Ugly thing: PID_MAX, NO_PID defined only in kernel
53 */
54#define NO_PID 100000
55
56enum SWRunType {
57 SRT_UNKNOWN = 1,
58 SRT_OPERATING_SYSTEM = 2,
--- 4 unchanged lines hidden (view full) ---
63
64enum SWRunStatus {
65 SRS_RUNNING = 1,
66 SRS_RUNNABLE = 2,
67 SRS_NOT_RUNNABLE = 3,
68 SRS_INVALID = 4
69};
70
71/*
72 * This structure is used to hold a SNMP table entry
73 * for both hrSWRunTable and hrSWRunPerfTable because
74 * hrSWRunPerfTable AUGMENTS hrSWRunTable
75 */
76struct swrun_entry {
77 int32_t index;
78 u_char name[64 + 1];
79 struct asn_oid id;
80 u_char path[128 + 1];
81 u_char parameters[128 + 1];
82 int32_t type; /* enum SWRunType */
83 int32_t status; /* enum SWRunStatus */
84 int32_t perfCPU;
85 int32_t perfMemory;
86#define HR_SWRUN_FOUND 0x001
87 uint32_t flags;
88 uint64_t r_tick; /* tick when entry refreshed */
89 TAILQ_ENTRY(swrun_entry) link;
90};
91TAILQ_HEAD(swrun_tbl, swrun_entry);
92
93/* the head of the list with hrSWRunTable's entries */
94static struct swrun_tbl swrun_tbl = TAILQ_HEAD_INITIALIZER(swrun_tbl);
--- 34 unchanged lines hidden (view full) ---
129static void
130swrun_entry_delete(struct swrun_entry *entry)
131{
132
133 assert(entry != NULL);
134
135 TAILQ_REMOVE(&swrun_tbl, entry, link);
136
137 free(entry);
138}
139
140/**
141 * Search one item by its index, return NULL if none found
142 */
143static struct swrun_entry *
144swrun_entry_find_by_index(int32_t idx)
145{
146 struct swrun_entry *entry;
147
148 TAILQ_FOREACH(entry, &swrun_tbl, link)
149 if (entry->index == idx)
150 return (entry);
151 return (NULL);
152}
153
154/**
155 * Translate the kernel's process status to the SNMP one.
156 */
157static enum SWRunStatus
158swrun_OS_get_proc_status(const struct kinfo_proc *kp)
159{
160
161 assert(kp != NULL);
162 if(kp == NULL) {
163 return (SRS_INVALID);
--- 30 unchanged lines hidden (view full) ---
194 * Make an SNMP table entry from a kernel one.
195 */
196static void
197kinfo_proc_to_swrun_entry(const struct kinfo_proc *kp,
198 struct swrun_entry *entry)
199{
200 char **argv = NULL;
201 uint64_t cpu_time = 0;
202
203 strlcpy((char*)entry->name, kp->ki_comm, sizeof(entry->name));
204
205 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
206
207 entry->path[0] = '\0';
208 entry->parameters[0] = '\0';
209
210 assert(hr_kd != NULL);
211
212 argv = kvm_getargv(hr_kd, kp, sizeof(entry->parameters) - 1);
213 if(argv != NULL){
214 memset(entry->parameters, '\0', sizeof(entry->parameters));
215
216 /*
217 * FIXME
218 * Path seems to not be available.
219 * Try to hack the info in argv[0];
220 * this argv is under control of the program so this info
221 * is not realiable
222 */
223 if(*argv != NULL && (*argv)[0] == '/') {
224 memset(entry->path, '\0', sizeof(entry->path));
225 strlcpy((char*)entry->path, *argv, sizeof(entry->path));
226 }
227
228 argv++; /* skip the first one which was used for path */
229
230 while (argv != NULL && *argv != NULL ) {
231 if (entry->parameters[0] != 0) {
232 /*
233 * add a space between parameters,
234 * except before the first one
235 */
236 strlcat((char *)entry->parameters,
237 " ", sizeof(entry->parameters));
238 }
239 strlcat((char *)entry->parameters, *argv,
240 sizeof(entry->parameters));
241 argv++;
242 }
243 }
244
245 entry->type = (int32_t)(IS_KERNPROC(kp) ? SRT_OPERATING_SYSTEM :
246 SRT_APPLICATION);
247
248 entry->status = (int32_t)swrun_OS_get_proc_status(kp);
249 cpu_time = kp->ki_runtime / 100000; /* centi-seconds */
250
--- 5 unchanged lines hidden (view full) ---
256
257/**
258 * Create a table entry for a KLD
259 */
260static void
261kld_file_stat_to_swrun(const struct kld_file_stat *kfs,
262 struct swrun_entry *entry)
263{
264
265 assert(kfs != NULL);
266 assert(entry != NULL);
267
268 strlcpy((char *)entry->name, kfs->name, sizeof(entry->name));
269
270 /* FIXME: can we find the location where the module was loaded from? */
271 entry->path[0] = '\0';
272
273 /* no parameters for kernel files (.ko) of for the kernel */
274 entry->parameters[0] = '\0';
275
276 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
277
278 if (strcmp(kfs->name, "kernel") == 0) {
279 entry->type = (int32_t)SRT_OPERATING_SYSTEM;
280 SWOSIndex = entry->index;
281 } else {
282 entry->type = (int32_t)SRT_DEVICE_DRIVER; /* well, not really */
283 }
284 entry->status = (int32_t)SRS_RUNNING;
--- 339 unchanged lines hidden (view full) ---
624 ret = SNMP_ERR_NOERROR;
625 switch (value->var.subs[sub - 1]) {
626
627 case LEAF_hrSWRunIndex:
628 value->v.integer = entry->index;
629 break;
630
631 case LEAF_hrSWRunName:
632 ret = string_get(value, entry->name, -1);
633 break;
634
635 case LEAF_hrSWRunID:
636 value->v.oid = entry->id;
637 break;
638
639 case LEAF_hrSWRunPath:
640 ret = string_get(value, entry->path, -1);
641 break;
642
643 case LEAF_hrSWRunParameters:
644 ret = string_get(value, entry->parameters, -1);
645 break;
646
647 case LEAF_hrSWRunType:
648 value->v.integer = entry->type;
649 break;
650
651 case LEAF_hrSWRunStatus:
652 value->v.integer = entry->status;
653 break;
654
655 default:
656 abort();
657 }
658 return (ret);
659}
660
661/**
662 * Scalar(s) in the SWRun group
663 */
664int
--- 62 unchanged lines hidden (view full) ---
727 case SNMP_OP_SET:
728 if ((entry = FIND_OBJECT_INT(&swrun_tbl,
729 &value->var, sub)) == NULL)
730 return (SNMP_ERR_NO_CREATION);
731 return (SNMP_ERR_NOT_WRITEABLE);
732
733 case SNMP_OP_ROLLBACK:
734 case SNMP_OP_COMMIT:
735 abort();
736 }
737 abort();
738
739 get:
740 switch (value->var.subs[sub - 1]) {
741
742 case LEAF_hrSWRunPerfCPU:
743 value->v.integer = entry->perfCPU;
744 return (SNMP_ERR_NOERROR);
745
746 case LEAF_hrSWRunPerfMem:
747 value->v.integer = entry->perfMemory;
748 return (SNMP_ERR_NOERROR);
749 }
750 abort();
751}
2 * Copyright (c) 2005-2006 The FreeBSD Project
3 * All rights reserved.
4 *
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
6 *
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
--- 12 unchanged lines hidden (view full) ---
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c 154860 2006-01-26 12:19:10Z harti $
30 *
31 * Host Resources MIB for SNMPd. Implementation for hrSWRunTable
32 */
33
34#include <sys/param.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37#include <sys/user.h>
--- 4 unchanged lines hidden (view full) ---
42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45
46#include "hostres_snmp.h"
47#include "hostres_oid.h"
48#include "hostres_tree.h"
49
50
51/*
52 * Ugly thing: PID_MAX, NO_PID defined only in kernel
53 */
54#define NO_PID 100000
55
56enum SWRunType {
57 SRT_UNKNOWN = 1,
58 SRT_OPERATING_SYSTEM = 2,
--- 4 unchanged lines hidden (view full) ---
63
64enum SWRunStatus {
65 SRS_RUNNING = 1,
66 SRS_RUNNABLE = 2,
67 SRS_NOT_RUNNABLE = 3,
68 SRS_INVALID = 4
69};
70
71/*
72 * This structure is used to hold a SNMP table entry
73 * for both hrSWRunTable and hrSWRunPerfTable because
74 * hrSWRunPerfTable AUGMENTS hrSWRunTable
75 */
76struct swrun_entry {
77 int32_t index;
78 u_char name[64 + 1];
79 struct asn_oid id;
80 u_char path[128 + 1];
81 u_char parameters[128 + 1];
82 int32_t type; /* enum SWRunType */
83 int32_t status; /* enum SWRunStatus */
84 int32_t perfCPU;
85 int32_t perfMemory;
86#define HR_SWRUN_FOUND 0x001
87 uint32_t flags;
88 uint64_t r_tick; /* tick when entry refreshed */
89 TAILQ_ENTRY(swrun_entry) link;
90};
91TAILQ_HEAD(swrun_tbl, swrun_entry);
92
93/* the head of the list with hrSWRunTable's entries */
94static struct swrun_tbl swrun_tbl = TAILQ_HEAD_INITIALIZER(swrun_tbl);
--- 34 unchanged lines hidden (view full) ---
129static void
130swrun_entry_delete(struct swrun_entry *entry)
131{
132
133 assert(entry != NULL);
134
135 TAILQ_REMOVE(&swrun_tbl, entry, link);
136
137 free(entry);
138}
139
140/**
141 * Search one item by its index, return NULL if none found
142 */
143static struct swrun_entry *
144swrun_entry_find_by_index(int32_t idx)
145{
146 struct swrun_entry *entry;
147
148 TAILQ_FOREACH(entry, &swrun_tbl, link)
149 if (entry->index == idx)
150 return (entry);
151 return (NULL);
152}
153
154/**
155 * Translate the kernel's process status to the SNMP one.
156 */
157static enum SWRunStatus
158swrun_OS_get_proc_status(const struct kinfo_proc *kp)
159{
160
161 assert(kp != NULL);
162 if(kp == NULL) {
163 return (SRS_INVALID);
--- 30 unchanged lines hidden (view full) ---
194 * Make an SNMP table entry from a kernel one.
195 */
196static void
197kinfo_proc_to_swrun_entry(const struct kinfo_proc *kp,
198 struct swrun_entry *entry)
199{
200 char **argv = NULL;
201 uint64_t cpu_time = 0;
202
203 strlcpy((char*)entry->name, kp->ki_comm, sizeof(entry->name));
204
205 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
206
207 entry->path[0] = '\0';
208 entry->parameters[0] = '\0';
209
210 assert(hr_kd != NULL);
211
212 argv = kvm_getargv(hr_kd, kp, sizeof(entry->parameters) - 1);
213 if(argv != NULL){
214 memset(entry->parameters, '\0', sizeof(entry->parameters));
215
216 /*
217 * FIXME
218 * Path seems to not be available.
219 * Try to hack the info in argv[0];
220 * this argv is under control of the program so this info
221 * is not realiable
222 */
223 if(*argv != NULL && (*argv)[0] == '/') {
224 memset(entry->path, '\0', sizeof(entry->path));
225 strlcpy((char*)entry->path, *argv, sizeof(entry->path));
226 }
227
228 argv++; /* skip the first one which was used for path */
229
230 while (argv != NULL && *argv != NULL ) {
231 if (entry->parameters[0] != 0) {
232 /*
233 * add a space between parameters,
234 * except before the first one
235 */
236 strlcat((char *)entry->parameters,
237 " ", sizeof(entry->parameters));
238 }
239 strlcat((char *)entry->parameters, *argv,
240 sizeof(entry->parameters));
241 argv++;
242 }
243 }
244
245 entry->type = (int32_t)(IS_KERNPROC(kp) ? SRT_OPERATING_SYSTEM :
246 SRT_APPLICATION);
247
248 entry->status = (int32_t)swrun_OS_get_proc_status(kp);
249 cpu_time = kp->ki_runtime / 100000; /* centi-seconds */
250
--- 5 unchanged lines hidden (view full) ---
256
257/**
258 * Create a table entry for a KLD
259 */
260static void
261kld_file_stat_to_swrun(const struct kld_file_stat *kfs,
262 struct swrun_entry *entry)
263{
264
265 assert(kfs != NULL);
266 assert(entry != NULL);
267
268 strlcpy((char *)entry->name, kfs->name, sizeof(entry->name));
269
270 /* FIXME: can we find the location where the module was loaded from? */
271 entry->path[0] = '\0';
272
273 /* no parameters for kernel files (.ko) of for the kernel */
274 entry->parameters[0] = '\0';
275
276 entry->id = oid_zeroDotZero; /* unknown id - FIXME */
277
278 if (strcmp(kfs->name, "kernel") == 0) {
279 entry->type = (int32_t)SRT_OPERATING_SYSTEM;
280 SWOSIndex = entry->index;
281 } else {
282 entry->type = (int32_t)SRT_DEVICE_DRIVER; /* well, not really */
283 }
284 entry->status = (int32_t)SRS_RUNNING;
--- 339 unchanged lines hidden (view full) ---
624 ret = SNMP_ERR_NOERROR;
625 switch (value->var.subs[sub - 1]) {
626
627 case LEAF_hrSWRunIndex:
628 value->v.integer = entry->index;
629 break;
630
631 case LEAF_hrSWRunName:
632 ret = string_get(value, entry->name, -1);
633 break;
634
635 case LEAF_hrSWRunID:
636 value->v.oid = entry->id;
637 break;
638
639 case LEAF_hrSWRunPath:
640 ret = string_get(value, entry->path, -1);
641 break;
642
643 case LEAF_hrSWRunParameters:
644 ret = string_get(value, entry->parameters, -1);
645 break;
646
647 case LEAF_hrSWRunType:
648 value->v.integer = entry->type;
649 break;
650
651 case LEAF_hrSWRunStatus:
652 value->v.integer = entry->status;
653 break;
654
655 default:
656 abort();
657 }
658 return (ret);
659}
660
661/**
662 * Scalar(s) in the SWRun group
663 */
664int
--- 62 unchanged lines hidden (view full) ---
727 case SNMP_OP_SET:
728 if ((entry = FIND_OBJECT_INT(&swrun_tbl,
729 &value->var, sub)) == NULL)
730 return (SNMP_ERR_NO_CREATION);
731 return (SNMP_ERR_NOT_WRITEABLE);
732
733 case SNMP_OP_ROLLBACK:
734 case SNMP_OP_COMMIT:
735 abort();
736 }
737 abort();
738
739 get:
740 switch (value->var.subs[sub - 1]) {
741
742 case LEAF_hrSWRunPerfCPU:
743 value->v.integer = entry->perfCPU;
744 return (SNMP_ERR_NOERROR);
745
746 case LEAF_hrSWRunPerfMem:
747 value->v.integer = entry->perfMemory;
748 return (SNMP_ERR_NOERROR);
749 }
750 abort();
751}