Merge branch 'collectd-3.11' into collectd-4.0
[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 #include "common.h"
25 #include "plugin.h"
26
27 #if defined(HAVE_LIBKSTAT)
28 # define TAPE_HAVE_READ 1
29 #else
30 # define TAPE_HAVE_READ 0
31 #endif
32
33 #if TAPE_HAVE_READ
34 #if defined(HAVE_LIBKSTAT)
35 #define MAX_NUMTAPE 256
36 extern kstat_ctl_t *kc;
37 static kstat_t *ksp[MAX_NUMTAPE];
38 static int numtape = 0;
39 #endif /* HAVE_LIBKSTAT */
40
41 static int tape_init (void)
42 {
43 #ifdef HAVE_LIBKSTAT
44         kstat_t *ksp_chain;
45
46         numtape = 0;
47
48         if (kc == NULL)
49                 return (-1);
50
51         for (numtape = 0, ksp_chain = kc->kc_chain;
52                         (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
53                         ksp_chain = ksp_chain->ks_next)
54         {
55                 if (strncmp (ksp_chain->ks_class, "tape", 4) )
56                         continue;
57                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
58                         continue;
59                 ksp[numtape++] = ksp_chain;
60         }
61 #endif
62
63         return (0);
64 }
65
66 static void tape_submit (const char *plugin_instance,
67                 const char *type,
68                 counter_t read, counter_t write)
69 {
70         value_t values[2];
71         value_list_t vl = VALUE_LIST_INIT;
72
73         values[0].counter = read;
74         values[1].counter = write;
75
76         vl.values = values;
77         vl.values_len = 2;
78         vl.time = time (NULL);
79         strcpy (vl.host, hostname_g);
80         strcpy (vl.plugin, "tape");
81         strncpy (vl.plugin_instance, plugin_instance,
82                         sizeof (vl.plugin_instance));
83
84         plugin_dispatch_values (type, &vl);
85 } /* void tape_submit */
86
87 static int tape_read (void)
88 {
89
90 #if defined(HAVE_LIBKSTAT)
91 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
92 #  define KIO_ROCTETS reads
93 #  define KIO_WOCTETS writes
94 #  define KIO_ROPS    nreads
95 #  define KIO_WOPS    nwrites
96 #  define KIO_RTIME   rtime
97 #  define KIO_WTIME   wtime
98 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
99 #  define KIO_ROCTETS nread
100 #  define KIO_WOCTETS nwritten
101 #  define KIO_ROPS    reads
102 #  define KIO_WOPS    writes
103 #  define KIO_RTIME   rtime
104 #  define KIO_WTIME   wtime
105 # else
106 #  error "kstat_io_t does not have the required members"
107 # endif
108         static kstat_io_t kio;
109         int i;
110
111         if (kc == NULL)
112                 return (-1);
113
114         if (numtape <= 0)
115                 return (-1);
116
117         for (i = 0; i < numtape; i++)
118         {
119                 if (kstat_read (kc, ksp[i], &kio) == -1)
120                         continue;
121
122                 if (strncmp (ksp[i]->ks_class, "tape", 4) == 0)
123                 {
124                         tape_submit (ksp[i]->ks_name, "tape_octets",
125                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
126                         tape_submit (ksp[i]->ks_name, "tape_ops",
127                                         kio.KIO_ROPS, kio.KIO_WOPS);
128                         /* FIXME: Convert this to microseconds if necessary */
129                         tape_submit (ksp[i]->ks_name, "tape_time",
130                                         kio.KIO_RTIME, kio.KIO_WTIME);
131                 }
132         }
133 #endif /* defined(HAVE_LIBKSTAT) */
134
135         return (0);
136 }
137 #endif /* TAPE_HAVE_READ */
138
139 void module_register (void)
140 {
141 #if TAPE_HAVE_READ
142         plugin_register_init ("tape", tape_init);
143         plugin_register_read ("tape", tape_read);
144 #endif /* TAPE_HAVE_READ */
145 }