1/*
2 * Copyright (c) 2004-2008 Hyperic, Inc.
3 * Copyright (c) 2009 SpringSource, Inc.
4 * Copyright (c) 2009-2010 VMware, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef SIGAR_H
20#define SIGAR_H
21
22/* System Information Gatherer And Reporter */
23
24#include <limits.h>
25#include <sigar_visibility.h>
26#include <stdint.h>
27#include <inttypes.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#if defined(_LP64)         || \
34    defined(__LP64__)      || \
35    defined(__64BIT__)     || \
36    defined(__powerpc64__) || \
37    defined(__osf__)
38#define SIGAR_64BIT
39#endif
40
41#define SIGAR_F_U64 "%"PRIu64
42
43   typedef int32_t sigar_int32_t;
44   typedef int64_t sigar_int64_t;
45   typedef uint32_t sigar_uint32_t;
46   typedef uint64_t sigar_uint64_t;
47
48#define SIGAR_FIELD_NOTIMPL -1
49
50#define SIGAR_OK 0
51#define SIGAR_START_ERROR 20000
52#define SIGAR_ENOTIMPL       (SIGAR_START_ERROR + 1)
53#define SIGAR_OS_START_ERROR (SIGAR_START_ERROR*2)
54
55#ifdef WIN32
56#   define SIGAR_ENOENT ERROR_FILE_NOT_FOUND
57#   define SIGAR_EACCES ERROR_ACCESS_DENIED
58#   define SIGAR_ENXIO  ERROR_BAD_DRIVER_LEVEL
59#else
60#   define SIGAR_ENOENT ENOENT
61#   define SIGAR_EACCES EACCES
62#   define SIGAR_ENXIO  ENXIO
63#endif
64
65#define SIGAR_DECLARE(type) SIGAR_PUBLIC_API type
66
67#if defined(PATH_MAX)
68#   define SIGAR_PATH_MAX PATH_MAX
69#elif defined(MAXPATHLEN)
70#   define SIGAR_PATH_MAX MAXPATHLEN
71#else
72#   define SIGAR_PATH_MAX 4096
73#endif
74
75#ifdef WIN32
76typedef sigar_uint64_t sigar_pid_t;
77typedef unsigned long sigar_uid_t;
78typedef unsigned long sigar_gid_t;
79#else
80#include <sys/types.h>
81typedef pid_t sigar_pid_t;
82typedef uid_t sigar_uid_t;
83typedef gid_t sigar_gid_t;
84#endif
85
86typedef struct sigar_t sigar_t;
87
88SIGAR_DECLARE(int) sigar_open(sigar_t **sigar);
89
90SIGAR_DECLARE(int) sigar_close(sigar_t *sigar);
91
92SIGAR_DECLARE(sigar_pid_t) sigar_pid_get(sigar_t *sigar);
93
94SIGAR_DECLARE(char *) sigar_strerror(sigar_t *sigar, int err);
95
96/* system memory info */
97
98typedef struct {
99    sigar_uint64_t
100        ram,
101        total,
102        used,
103        free,
104        actual_used,
105        actual_free;
106    double used_percent;
107    double free_percent;
108} sigar_mem_t;
109
110SIGAR_DECLARE(int) sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem);
111
112typedef struct {
113    sigar_uint64_t
114        total,
115        used,
116        free,
117        page_in,
118        page_out;
119} sigar_swap_t;
120
121SIGAR_DECLARE(int) sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap);
122
123typedef struct {
124    sigar_uint64_t
125        user,
126        sys,
127        nice,
128        idle,
129        wait,
130        irq,
131        soft_irq,
132        stolen,
133        total;
134} sigar_cpu_t;
135
136SIGAR_DECLARE(int) sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu);
137
138typedef struct {
139    char vendor[128];
140    char model[128];
141    int mhz;
142    int mhz_max;
143    int mhz_min;
144    sigar_uint64_t cache_size;
145    int total_sockets;
146    int total_cores;
147    int cores_per_socket;
148} sigar_cpu_info_t;
149
150typedef struct {
151    double uptime;
152} sigar_uptime_t;
153
154typedef struct {
155    unsigned long number;
156    unsigned long size;
157    sigar_pid_t *data;
158} sigar_proc_list_t;
159
160SIGAR_DECLARE(int) sigar_proc_list_get(sigar_t *sigar,
161                                       sigar_proc_list_t *proclist);
162
163SIGAR_DECLARE(int) sigar_proc_list_destroy(sigar_t *sigar,
164                                           sigar_proc_list_t *proclist);
165
166
167typedef struct {
168    sigar_uint64_t
169        size,
170        resident,
171        share,
172        minor_faults,
173        major_faults,
174        page_faults;
175} sigar_proc_mem_t;
176
177SIGAR_DECLARE(int) sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
178                                      sigar_proc_mem_t *procmem);
179
180typedef struct {
181    sigar_uid_t uid;
182    sigar_gid_t gid;
183    sigar_uid_t euid;
184    sigar_gid_t egid;
185} sigar_proc_cred_t;
186
187typedef struct {
188    sigar_uint64_t
189        start_time,
190        user,
191        sys,
192        total;
193} sigar_proc_time_t;
194
195// Not used externally
196int sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
197                                       sigar_proc_time_t *proctime);
198
199typedef struct {
200    /* must match sigar_proc_time_t fields */
201    sigar_uint64_t
202        start_time,
203        user,
204        sys,
205        total;
206    sigar_uint64_t last_time;
207    double percent;
208} sigar_proc_cpu_t;
209
210SIGAR_DECLARE(int) sigar_proc_cpu_get(sigar_t *sigar, sigar_pid_t pid,
211                                      sigar_proc_cpu_t *proccpu);
212
213#define SIGAR_PROC_STATE_SLEEP  'S'
214#define SIGAR_PROC_STATE_RUN    'R'
215#define SIGAR_PROC_STATE_STOP   'T'
216#define SIGAR_PROC_STATE_ZOMBIE 'Z'
217#define SIGAR_PROC_STATE_IDLE   'D'
218
219#define SIGAR_PROC_NAME_LEN 128
220
221typedef struct {
222    char name[SIGAR_PROC_NAME_LEN];
223    char state;
224    sigar_pid_t ppid;
225    int tty;
226    int priority;
227    int nice;
228    int processor;
229    sigar_uint64_t threads;
230} sigar_proc_state_t;
231
232SIGAR_DECLARE(int) sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid,
233                                        sigar_proc_state_t *procstate);
234
235typedef struct {
236    unsigned long number;
237    unsigned long size;
238    char **data;
239} sigar_proc_args_t;
240
241typedef struct {
242    void *data; /* user data */
243
244    int (*module_getter)(void *, char *, int);
245} sigar_proc_modules_t;
246
247
248typedef enum {
249    SIGAR_FSTYPE_UNKNOWN,
250    SIGAR_FSTYPE_NONE,
251    SIGAR_FSTYPE_LOCAL_DISK,
252    SIGAR_FSTYPE_NETWORK,
253    SIGAR_FSTYPE_RAM_DISK,
254    SIGAR_FSTYPE_CDROM,
255    SIGAR_FSTYPE_SWAP,
256    SIGAR_FSTYPE_MAX
257} sigar_file_system_type_e;
258
259#define SIGAR_FS_NAME_LEN SIGAR_PATH_MAX
260#define SIGAR_FS_INFO_LEN 256
261
262typedef struct {
263    char dir_name[SIGAR_FS_NAME_LEN];
264    char dev_name[SIGAR_FS_NAME_LEN];
265    char type_name[SIGAR_FS_INFO_LEN];     /* e.g. "local" */
266    char sys_type_name[SIGAR_FS_INFO_LEN]; /* e.g. "ext3" */
267    char options[SIGAR_FS_INFO_LEN];
268    sigar_file_system_type_e type;
269    unsigned long flags;
270} sigar_file_system_t;
271
272typedef struct {
273    unsigned long number;
274    unsigned long size;
275    sigar_file_system_t *data;
276} sigar_file_system_list_t;
277
278SIGAR_DECLARE(int)
279sigar_file_system_list_get(sigar_t *sigar,
280                           sigar_file_system_list_t *fslist);
281
282SIGAR_DECLARE(int)
283sigar_file_system_list_destroy(sigar_t *sigar,
284                               sigar_file_system_list_t *fslist);
285
286typedef struct {
287    sigar_uint64_t reads;
288    sigar_uint64_t writes;
289    sigar_uint64_t write_bytes;
290    sigar_uint64_t read_bytes;
291    sigar_uint64_t rtime;
292    sigar_uint64_t wtime;
293    sigar_uint64_t qtime;
294    sigar_uint64_t time;
295    sigar_uint64_t snaptime;
296    double service_time;
297    double queue;
298} sigar_disk_usage_t;
299
300typedef struct {
301    sigar_disk_usage_t disk;
302    double use_percent;
303    sigar_uint64_t total;
304    sigar_uint64_t free;
305    sigar_uint64_t used;
306    sigar_uint64_t avail;
307    sigar_uint64_t files;
308    sigar_uint64_t free_files;
309} sigar_file_system_usage_t;
310
311#undef SIGAR_DISK_USAGE_T
312
313
314
315
316typedef struct {
317    enum {
318        SIGAR_AF_UNSPEC,
319        SIGAR_AF_INET,
320        SIGAR_AF_INET6,
321        SIGAR_AF_LINK
322    } family;
323    union {
324        sigar_uint32_t in;
325        sigar_uint32_t in6[4];
326        unsigned char mac[8];
327    } addr;
328} sigar_net_address_t;
329
330#define SIGAR_INET6_ADDRSTRLEN 46
331
332#define SIGAR_MAXDOMAINNAMELEN 256
333#define SIGAR_MAXHOSTNAMELEN 256
334
335
336
337/*
338 * platforms define most of these "standard" flags,
339 * but of course, with different values in some cases.
340 */
341#define SIGAR_IFF_UP          0x1
342#define SIGAR_IFF_BROADCAST   0x2
343#define SIGAR_IFF_DEBUG       0x4
344#define SIGAR_IFF_LOOPBACK    0x8
345#define SIGAR_IFF_POINTOPOINT 0x10
346#define SIGAR_IFF_NOTRAILERS  0x20
347#define SIGAR_IFF_RUNNING     0x40
348#define SIGAR_IFF_NOARP       0x80
349#define SIGAR_IFF_PROMISC     0x100
350#define SIGAR_IFF_ALLMULTI    0x200
351#define SIGAR_IFF_MULTICAST   0x800
352#define SIGAR_IFF_SLAVE       0x1000
353#define SIGAR_IFF_MASTER      0x2000
354#define SIGAR_IFF_DYNAMIC     0x4000
355
356#define SIGAR_NULL_HWADDR "00:00:00:00:00:00"
357
358/* scope values from linux-2.6/include/net/ipv6.h */
359#define SIGAR_IPV6_ADDR_ANY        0x0000
360#define SIGAR_IPV6_ADDR_UNICAST    0x0001
361#define SIGAR_IPV6_ADDR_MULTICAST  0x0002
362#define SIGAR_IPV6_ADDR_LOOPBACK   0x0010
363#define SIGAR_IPV6_ADDR_LINKLOCAL  0x0020
364#define SIGAR_IPV6_ADDR_SITELOCAL  0x0040
365#define SIGAR_IPV6_ADDR_COMPATv4   0x0080
366
367typedef struct {
368    char name[16];
369    char type[64];
370    char description[256];
371    sigar_net_address_t hwaddr;
372    sigar_net_address_t address;
373    sigar_net_address_t destination;
374    sigar_net_address_t broadcast;
375    sigar_net_address_t netmask;
376    sigar_net_address_t address6;
377    int prefix6_length;
378    int scope6;
379    sigar_uint64_t
380        flags,
381        mtu,
382        metric;
383    int tx_queue_len;
384} sigar_net_interface_config_t;
385
386SIGAR_DECLARE(int)
387sigar_net_interface_config_get(sigar_t *sigar,
388                               const char *name,
389                               sigar_net_interface_config_t *ifconfig);
390
391SIGAR_DECLARE(int)
392sigar_net_interface_config_primary_get(sigar_t *sigar,
393                                       sigar_net_interface_config_t *ifconfig);
394
395
396typedef struct {
397    unsigned long number;
398    unsigned long size;
399    char **data;
400} sigar_net_interface_list_t;
401
402SIGAR_DECLARE(int)
403sigar_net_interface_list_get(sigar_t *sigar,
404                             sigar_net_interface_list_t *iflist);
405
406SIGAR_DECLARE(int)
407sigar_net_interface_list_destroy(sigar_t *sigar,
408                                 sigar_net_interface_list_t *iflist);
409
410#define SIGAR_NETCONN_CLIENT 0x01
411#define SIGAR_NETCONN_SERVER 0x02
412
413#define SIGAR_NETCONN_TCP  0x10
414#define SIGAR_NETCONN_UDP  0x20
415#define SIGAR_NETCONN_RAW  0x40
416#define SIGAR_NETCONN_UNIX 0x80
417
418enum {
419    SIGAR_TCP_ESTABLISHED = 1,
420    SIGAR_TCP_SYN_SENT,
421    SIGAR_TCP_SYN_RECV,
422    SIGAR_TCP_FIN_WAIT1,
423    SIGAR_TCP_FIN_WAIT2,
424    SIGAR_TCP_TIME_WAIT,
425    SIGAR_TCP_CLOSE,
426    SIGAR_TCP_CLOSE_WAIT,
427    SIGAR_TCP_LAST_ACK,
428    SIGAR_TCP_LISTEN,
429    SIGAR_TCP_CLOSING,
430    SIGAR_TCP_IDLE,
431    SIGAR_TCP_BOUND,
432    SIGAR_TCP_UNKNOWN
433};
434
435typedef struct {
436    unsigned long local_port;
437    sigar_net_address_t local_address;
438    unsigned long remote_port;
439    sigar_net_address_t remote_address;
440    sigar_uid_t uid;
441    unsigned long inode;
442    int type;
443    int state;
444    unsigned long send_queue;
445    unsigned long receive_queue;
446} sigar_net_connection_t;
447
448typedef struct {
449    unsigned long number;
450    unsigned long size;
451    sigar_net_connection_t *data;
452} sigar_net_connection_list_t;
453
454SIGAR_DECLARE(int)
455sigar_net_connection_list_get(sigar_t *sigar,
456                              sigar_net_connection_list_t *connlist,
457                              int flags);
458
459SIGAR_DECLARE(int)
460sigar_net_connection_list_destroy(sigar_t *sigar,
461                                  sigar_net_connection_list_t *connlist);
462
463typedef struct sigar_net_connection_walker_t sigar_net_connection_walker_t;
464
465/* alternative to sigar_net_connection_list_get */
466struct sigar_net_connection_walker_t {
467    sigar_t *sigar;
468    int flags;
469    void *data; /* user data */
470    int (*add_connection)(sigar_net_connection_walker_t *walker,
471                          sigar_net_connection_t *connection);
472};
473
474SIGAR_DECLARE(int)
475sigar_net_connection_walk(sigar_net_connection_walker_t *walker);
476
477
478
479/* TCP-MIB */
480typedef struct {
481    sigar_uint64_t active_opens;
482    sigar_uint64_t passive_opens;
483    sigar_uint64_t attempt_fails;
484    sigar_uint64_t estab_resets;
485    sigar_uint64_t curr_estab;
486    sigar_uint64_t in_segs;
487    sigar_uint64_t out_segs;
488    sigar_uint64_t retrans_segs;
489    sigar_uint64_t in_errs;
490    sigar_uint64_t out_rsts;
491} sigar_tcp_t;
492
493
494#define SIGAR_SYS_INFO_LEN SIGAR_MAXHOSTNAMELEN /* more than enough */
495
496typedef struct {
497    char name[SIGAR_SYS_INFO_LEN]; /* canonicalized sysname */
498    char version[SIGAR_SYS_INFO_LEN]; /* utsname.release */
499    char arch[SIGAR_SYS_INFO_LEN];
500    char machine[SIGAR_SYS_INFO_LEN];
501    char description[SIGAR_SYS_INFO_LEN];
502    char patch_level[SIGAR_SYS_INFO_LEN];
503    char vendor[SIGAR_SYS_INFO_LEN];
504    char vendor_version[SIGAR_SYS_INFO_LEN];
505    char vendor_name[SIGAR_SYS_INFO_LEN];  /* utsname.sysname */
506    char vendor_code_name[SIGAR_SYS_INFO_LEN];
507} sigar_sys_info_t;
508
509
510#define SIGAR_FQDN_LEN 512
511
512SIGAR_DECLARE(int) sigar_rpc_ping(char *hostname,
513                                  int protocol,
514                                  unsigned long program,
515                                  unsigned long version);
516
517SIGAR_DECLARE(char *) sigar_rpc_strerror(int err);
518
519#ifdef __cplusplus
520}
521#endif
522
523#endif
524