| 1 |
/* |
| 2 |
* mach interface |
| 3 |
* Apple darwin specific |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-includes.h> |
| 7 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 8 |
#include <net-snmp/agent/hardware/cpu.h> |
| 9 |
|
| 10 |
#include <mach/mach.h> |
| 11 |
|
| 12 |
#include <errno.h> |
| 13 |
|
| 14 |
static host_name_port_t host; |
| 15 |
static struct host_basic_info hi; |
| 16 |
|
| 17 |
/* |
| 18 |
* Initialise the list of CPUs on the system |
| 19 |
* (including descriptions) |
| 20 |
*/ |
| 21 |
void init_cpu_mach( void ) { |
| 22 |
int ret, i; |
| 23 |
mach_msg_type_number_t size; |
| 24 |
char *cpu_type, *cpu_subtype; |
| 25 |
netsnmp_cpu_info *cpu; |
| 26 |
|
| 27 |
cpu = netsnmp_cpu_get_byIdx( -1, 1 ); |
| 28 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 29 |
|
| 30 |
host = mach_host_self(); |
| 31 |
size = sizeof(hi); |
| 32 |
ret = host_info(host, HOST_BASIC_INFO, (host_info_t)&hi, &size); |
| 33 |
if (ret != KERN_SUCCESS) { |
| 34 |
snmp_log(LOG_ERR, "HOST_BASIC_INFO: %s - %s\n", mach_error_string(ret), strerror(errno)); |
| 35 |
return; |
| 36 |
} |
| 37 |
for ( i = 0; i < hi.avail_cpus; i++) { |
| 38 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 39 |
cpu->status = 2; /* running */ |
| 40 |
sprintf( cpu->name, "cpu%d", i ); |
| 41 |
/* XXX get per-cpu type? Could it be different? */ |
| 42 |
slot_name(hi.cpu_type, hi.cpu_subtype, &cpu_type, &cpu_subtype); |
| 43 |
sprintf( cpu->descr, "%s - %s", cpu_type, cpu_subtype ); |
| 44 |
} |
| 45 |
cpu_num = hi.avail_cpus; |
| 46 |
} |
| 47 |
|
| 48 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) { |
| 49 |
int i, ret; |
| 50 |
mach_msg_type_number_t numcpu, nummsg; |
| 51 |
processor_cpu_load_info_t pcli; |
| 52 |
vm_statistics_data_t vmstat; |
| 53 |
netsnmp_cpu_info *cpu, *cpu0; |
| 54 |
|
| 55 |
/* |
| 56 |
* Load the latest CPU usage statistics |
| 57 |
*/ |
| 58 |
ret = host_processor_info(host, PROCESSOR_CPU_LOAD_INFO, &numcpu, |
| 59 |
(processor_info_array_t *)&pcli, |
| 60 |
&nummsg); |
| 61 |
if (ret != KERN_SUCCESS) { |
| 62 |
snmp_log(LOG_ERR, "PROCESSOR_CPU_LOAD_INFO: %s - %s\n", mach_error_string(ret), strerror(errno)); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
cpu0 = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 67 |
for ( i = 0; i < numcpu; i++) { |
| 68 |
/* Note: using sys_ticks so that CPUSYSTEM gets calculated right. */ |
| 69 |
/* many collectors use sys2_ticks instead. */ |
| 70 |
if (i == 0) { |
| 71 |
cpu0->user_ticks = pcli[i].cpu_ticks[CPU_STATE_USER]; |
| 72 |
cpu0->nice_ticks = pcli[i].cpu_ticks[CPU_STATE_NICE]; |
| 73 |
cpu0->sys_ticks = pcli[i].cpu_ticks[CPU_STATE_SYSTEM]; |
| 74 |
cpu0->idle_ticks = pcli[i].cpu_ticks[CPU_STATE_IDLE]; |
| 75 |
} else { |
| 76 |
cpu0->user_ticks += pcli[i].cpu_ticks[CPU_STATE_USER]; |
| 77 |
cpu0->nice_ticks += pcli[i].cpu_ticks[CPU_STATE_NICE]; |
| 78 |
cpu0->sys_ticks += pcli[i].cpu_ticks[CPU_STATE_SYSTEM]; |
| 79 |
cpu0->idle_ticks += pcli[i].cpu_ticks[CPU_STATE_IDLE]; |
| 80 |
} |
| 81 |
cpu = netsnmp_cpu_get_byIdx( i, 0 ); |
| 82 |
if (cpu == NULL) { |
| 83 |
snmp_log(LOG_ERR, "forgot to create cpu #%d\n", i); |
| 84 |
continue; |
| 85 |
} |
| 86 |
cpu->user_ticks = pcli[i].cpu_ticks[CPU_STATE_USER]; |
| 87 |
cpu->nice_ticks = pcli[i].cpu_ticks[CPU_STATE_NICE]; |
| 88 |
cpu->sys_ticks = pcli[i].cpu_ticks[CPU_STATE_SYSTEM]; |
| 89 |
cpu->idle_ticks = pcli[i].cpu_ticks[CPU_STATE_IDLE]; |
| 90 |
/* kern_ticks, intrpt_ticks, wait_ticks, sirq_ticks unused */ |
| 91 |
} |
| 92 |
ret = vm_deallocate(mach_task_self(), (vm_address_t)pcli, nummsg * sizeof(int)); |
| 93 |
if (ret != KERN_SUCCESS) { |
| 94 |
snmp_log(LOG_ERR, "vm_deallocate: %s - %s\n", mach_error_string(ret), strerror(errno)); |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* Interrupt/Context Switch statistics |
| 99 |
* XXX - Do these really belong here ? |
| 100 |
*/ |
| 101 |
/* Darwin doesn't keep paging stats per CPU. */ |
| 102 |
nummsg = HOST_VM_INFO_COUNT; |
| 103 |
ret = host_statistics(host, HOST_VM_INFO, (host_info_t)&vmstat, &nummsg); |
| 104 |
if (ret != KERN_SUCCESS) { |
| 105 |
snmp_log(LOG_ERR, "HOST_VM_INFO: %s - %s\n", mach_error_string(ret), strerror(errno)); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
cpu0->pageIn = vmstat.pageins; |
| 109 |
cpu0->pageOut = vmstat.pageouts; |
| 110 |
/* not implemented: swapIn, swapOut, nInterrupts, nCtxSwitches */ |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
|