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