f59b7ea6b3860dc3892269b4455e398f31943323
[collectd.git] / src / tape.c
1 /**
2  * collectd - src/tape.c
3  * Copyright (C) 2005,2006  Scott Garrett
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Scott Garrett <sgarrett at technomancer.com>
21  **/
22
23 #include "collectd.h"
24
25 #include "common.h"
26 #include "plugin.h"
27
28 #if !HAVE_LIBKSTAT
29 #error "No applicable input method."
30 #endif
31
32 #if HAVE_KSTAT_H
33 #include <kstat.h>
34 #endif
35
36 #define MAX_NUMTAPE 256
37 extern kstat_ctl_t *kc;
38 static kstat_t *ksp[MAX_NUMTAPE];
39 static int numtape = 0;
40
41 static int tape_init(void) {
42   kstat_t *ksp_chain;
43
44   numtape = 0;
45
46   if (kc == NULL)
47     return -1;
48
49   for (numtape = 0, ksp_chain = kc->kc_chain;
50        (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
51        ksp_chain = ksp_chain->ks_next) {
52     if (strncmp(ksp_chain->ks_class, "tape", 4))
53       continue;
54     if (ksp_chain->ks_type != KSTAT_TYPE_IO)
55       continue;
56     ksp[numtape++] = ksp_chain;
57   }
58
59   return 0;
60 } /* int tape_init */
61
62 static void tape_submit(const char *plugin_instance, const char *type,
63                         derive_t read, derive_t write) {
64   value_list_t vl = VALUE_LIST_INIT;
65   value_t values[] = {
66       {.derive = read}, {.derive = write},
67   };
68
69   vl.values = values;
70   vl.values_len = STATIC_ARRAY_SIZE(values);
71   sstrncpy(vl.plugin, "tape", sizeof(vl.plugin));
72   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
73   sstrncpy(vl.type, type, sizeof(vl.type));
74
75   plugin_dispatch_values(&vl);
76 } /* void tape_submit */
77
78 static int tape_read(void) {
79
80 #if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
81 #define KIO_ROCTETS reads
82 #define KIO_WOCTETS writes
83 #define KIO_ROPS nreads
84 #define KIO_WOPS nwrites
85 #define KIO_RTIME rtime
86 #define KIO_WTIME wtime
87 #elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES &&                    \
88     HAVE_KSTAT_IO_T_WTIME
89 #define KIO_ROCTETS nread
90 #define KIO_WOCTETS nwritten
91 #define KIO_ROPS reads
92 #define KIO_WOPS writes
93 #define KIO_RTIME rtime
94 #define KIO_WTIME wtime
95 #else
96 #error "kstat_io_t does not have the required members"
97 #endif
98   static kstat_io_t kio;
99
100   if (kc == NULL)
101     return -1;
102
103   if (numtape <= 0)
104     return -1;
105
106   for (int i = 0; i < numtape; i++) {
107     if (kstat_read(kc, ksp[i], &kio) == -1)
108       continue;
109
110     if (strncmp(ksp[i]->ks_class, "tape", 4) == 0) {
111       tape_submit(ksp[i]->ks_name, "tape_octets", kio.KIO_ROCTETS,
112                   kio.KIO_WOCTETS);
113       tape_submit(ksp[i]->ks_name, "tape_ops", kio.KIO_ROPS, kio.KIO_WOPS);
114       /* FIXME: Convert this to microseconds if necessary */
115       tape_submit(ksp[i]->ks_name, "tape_time", kio.KIO_RTIME, kio.KIO_WTIME);
116     }
117   }
118
119   return 0;
120 }
121
122 void module_register(void) {
123   plugin_register_init("tape", tape_init);
124   plugin_register_read("tape", tape_read);
125 }