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 160341 2006-07-14 09:07:56Z 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 * Ugly thing: PID_MAX, NO_PID defined only in kernel 52 */ 53#define NO_PID 100000 54 55enum SWRunType { 56 SRT_UNKNOWN = 1, 57 SRT_OPERATING_SYSTEM = 2, --- 4 unchanged lines hidden (view full) --- 62 63enum SWRunStatus { 64 SRS_RUNNING = 1, 65 SRS_RUNNABLE = 2, 66 SRS_NOT_RUNNABLE = 3, 67 SRS_INVALID = 4 68}; 69 |
70/* Maximum lengths for the strings according to the MIB */ 71#define SWR_NAME_MLEN (64 + 1) 72#define SWR_PATH_MLEN (128 + 1) 73#define SWR_PARAM_MLEN (128 + 1) 74 |
75/* 76 * This structure is used to hold a SNMP table entry 77 * for both hrSWRunTable and hrSWRunPerfTable because 78 * hrSWRunPerfTable AUGMENTS hrSWRunTable 79 */ 80struct swrun_entry { 81 int32_t index; |
82 u_char *name; /* it may be NULL */ 83 const struct asn_oid *id; 84 u_char *path; /* it may be NULL */ 85 u_char *parameters; /* it may be NULL */ |
86 int32_t type; /* enum SWRunType */ 87 int32_t status; /* enum SWRunStatus */ 88 int32_t perfCPU; 89 int32_t perfMemory; |
90#define HR_SWRUN_FOUND 0x001 |
91 uint32_t flags; 92 uint64_t r_tick; /* tick when entry refreshed */ 93 TAILQ_ENTRY(swrun_entry) link; 94}; 95TAILQ_HEAD(swrun_tbl, swrun_entry); 96 97/* the head of the list with hrSWRunTable's entries */ 98static struct swrun_tbl swrun_tbl = TAILQ_HEAD_INITIALIZER(swrun_tbl); --- 34 unchanged lines hidden (view full) --- 133static void 134swrun_entry_delete(struct swrun_entry *entry) 135{ 136 137 assert(entry != NULL); 138 139 TAILQ_REMOVE(&swrun_tbl, entry, link); 140 |
141 free(entry->name); 142 free(entry->path); 143 free(entry->parameters); |
144 free(entry); 145} 146 147/** 148 * Search one item by its index, return NULL if none found 149 */ 150static struct swrun_entry * 151swrun_entry_find_by_index(int32_t idx) 152{ 153 struct swrun_entry *entry; 154 155 TAILQ_FOREACH(entry, &swrun_tbl, link) 156 if (entry->index == idx) 157 return (entry); 158 return (NULL); 159} 160 161/** |
162 * Translate the kernel's process status to SNMP. |
163 */ 164static enum SWRunStatus 165swrun_OS_get_proc_status(const struct kinfo_proc *kp) 166{ 167 168 assert(kp != NULL); 169 if(kp == NULL) { 170 return (SRS_INVALID); --- 30 unchanged lines hidden (view full) --- 201 * Make an SNMP table entry from a kernel one. 202 */ 203static void 204kinfo_proc_to_swrun_entry(const struct kinfo_proc *kp, 205 struct swrun_entry *entry) 206{ 207 char **argv = NULL; 208 uint64_t cpu_time = 0; |
209 size_t pname_len; |
210 |
211 pname_len = strlen(kp->ki_comm) + 1; 212 entry->name = reallocf(entry->name, pname_len); 213 if (entry->name != NULL) 214 strlcpy(entry->name, kp->ki_comm, pname_len); |
215 |
216 entry->id = &oid_zeroDotZero; /* unknown id - FIXME */ |
217 |
218 assert(hr_kd != NULL); 219 |
220 argv = kvm_getargv(hr_kd, kp, SWR_PARAM_MLEN - 1); |
221 if(argv != NULL){ |
222 u_char param[SWR_PARAM_MLEN]; |
223 |
224 memset(param, '\0', sizeof(param)); 225 |
226 /* 227 * FIXME 228 * Path seems to not be available. 229 * Try to hack the info in argv[0]; 230 * this argv is under control of the program so this info 231 * is not realiable 232 */ 233 if(*argv != NULL && (*argv)[0] == '/') { |
234 size_t path_len; 235 236 path_len = strlen(*argv) + 1; 237 if (path_len > SWR_PATH_MLEN) 238 path_len = SWR_PATH_MLEN; 239 240 entry->path = reallocf(entry->path, path_len); 241 if (entry->path != NULL) { 242 memset(entry->path, '\0', path_len); 243 strlcpy((char*)entry->path, *argv, path_len); 244 } |
245 } 246 247 argv++; /* skip the first one which was used for path */ 248 249 while (argv != NULL && *argv != NULL ) { |
250 if (param[0] != 0) { |
251 /* 252 * add a space between parameters, 253 * except before the first one 254 */ |
255 strlcat((char *)param, " ", sizeof(param)); |
256 } |
257 strlcat((char *)param, *argv, sizeof(param)); |
258 argv++; 259 } |
260 /* reuse pname_len */ 261 pname_len = strlen(param) + 1; 262 if (pname_len > SWR_PARAM_MLEN) 263 pname_len = SWR_PARAM_MLEN; 264 265 entry->parameters = reallocf(entry->parameters, pname_len); 266 strlcpy(entry->parameters, param, pname_len); |
267 } 268 269 entry->type = (int32_t)(IS_KERNPROC(kp) ? SRT_OPERATING_SYSTEM : 270 SRT_APPLICATION); 271 272 entry->status = (int32_t)swrun_OS_get_proc_status(kp); 273 cpu_time = kp->ki_runtime / 100000; /* centi-seconds */ 274 --- 5 unchanged lines hidden (view full) --- 280 281/** 282 * Create a table entry for a KLD 283 */ 284static void 285kld_file_stat_to_swrun(const struct kld_file_stat *kfs, 286 struct swrun_entry *entry) 287{ |
288 size_t name_len; |
289 290 assert(kfs != NULL); 291 assert(entry != NULL); 292 |
293 name_len = strlen(kfs->name) + 1; 294 if (name_len > SWR_NAME_MLEN) 295 name_len = SWR_NAME_MLEN; |
296 |
297 entry->name = reallocf(entry->name, name_len); 298 if (entry->name != NULL) 299 strlcpy((char *)entry->name, kfs->name, name_len); 300 |
301 /* FIXME: can we find the location where the module was loaded from? */ |
302 entry->path = NULL; |
303 304 /* no parameters for kernel files (.ko) of for the kernel */ |
305 entry->parameters = NULL; |
306 |
307 entry->id = &oid_zeroDotZero; /* unknown id - FIXME */ |
308 309 if (strcmp(kfs->name, "kernel") == 0) { 310 entry->type = (int32_t)SRT_OPERATING_SYSTEM; 311 SWOSIndex = entry->index; 312 } else { 313 entry->type = (int32_t)SRT_DEVICE_DRIVER; /* well, not really */ 314 } 315 entry->status = (int32_t)SRS_RUNNING; --- 339 unchanged lines hidden (view full) --- 655 ret = SNMP_ERR_NOERROR; 656 switch (value->var.subs[sub - 1]) { 657 658 case LEAF_hrSWRunIndex: 659 value->v.integer = entry->index; 660 break; 661 662 case LEAF_hrSWRunName: |
663 if (entry->name != NULL) 664 ret = string_get(value, entry->name, -1); 665 else 666 ret = string_get(value, "", -1); 667 break; |
668 669 case LEAF_hrSWRunID: |
670 assert(entry->id != NULL); 671 value->v.oid = *entry->id; 672 break; |
673 |
674 case LEAF_hrSWRunPath: 675 if (entry->path != NULL) 676 ret = string_get(value, entry->path, -1); 677 else 678 ret = string_get(value, "", -1); 679 break; |
680 681 case LEAF_hrSWRunParameters: |
682 if (entry->parameters != NULL) 683 ret = string_get(value, entry->parameters, -1); 684 else 685 ret = string_get(value, "", -1); 686 break; |
687 688 case LEAF_hrSWRunType: |
689 value->v.integer = entry->type; |
690 break; 691 692 case LEAF_hrSWRunStatus: |
693 value->v.integer = entry->status; |
694 break; 695 696 default: |
697 abort(); |
698 } 699 return (ret); 700} 701 702/** 703 * Scalar(s) in the SWRun group 704 */ 705int --- 62 unchanged lines hidden (view full) --- 768 case SNMP_OP_SET: 769 if ((entry = FIND_OBJECT_INT(&swrun_tbl, 770 &value->var, sub)) == NULL) 771 return (SNMP_ERR_NO_CREATION); 772 return (SNMP_ERR_NOT_WRITEABLE); 773 774 case SNMP_OP_ROLLBACK: 775 case SNMP_OP_COMMIT: |
776 abort(); |
777 } 778 abort(); 779 780 get: 781 switch (value->var.subs[sub - 1]) { 782 783 case LEAF_hrSWRunPerfCPU: 784 value->v.integer = entry->perfCPU; 785 return (SNMP_ERR_NOERROR); 786 787 case LEAF_hrSWRunPerfMem: 788 value->v.integer = entry->perfMemory; |
789 return (SNMP_ERR_NOERROR); |
790 } 791 abort(); 792} |