2 * collectd - src/tape.c
3 * Copyright (C) 2005,2006 Scott Garrett
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.
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.
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
20 * Scott Garrett <sgarrett at technomancer.com>
27 #if defined(HAVE_LIBKSTAT)
28 # define TAPE_HAVE_READ 1
30 # define TAPE_HAVE_READ 0
33 /* 2^34 = 17179869184 = ~17.2GByte/s */
34 static data_source_t octets_dsrc[2] =
36 {"read", DS_TYPE_COUNTER, 0, 17179869183.0},
37 {"write", DS_TYPE_COUNTER, 0, 17179869183.0}
40 static data_set_t octets_ds =
42 "tape_octets", 2, octets_dsrc
45 static data_source_t operations_dsrc[2] =
47 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
48 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
51 static data_set_t operations_ds =
53 "tape_ops", 2, operations_dsrc
56 static data_source_t merged_dsrc[2] =
58 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
59 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
62 static data_set_t merged_ds =
64 "tape_merged", 2, merged_dsrc
67 /* max is 1000000us per second. */
68 static data_source_t time_dsrc[2] =
70 {"read", DS_TYPE_COUNTER, 0, 1000000.0},
71 {"write", DS_TYPE_COUNTER, 0, 1000000.0}
74 static data_set_t time_ds =
76 "tape_time", 2, time_dsrc
80 #if defined(HAVE_LIBKSTAT)
81 #define MAX_NUMTAPE 256
82 extern kstat_ctl_t *kc;
83 static kstat_t *ksp[MAX_NUMTAPE];
84 static int numtape = 0;
85 #endif /* HAVE_LIBKSTAT */
87 static int tape_init (void)
97 for (numtape = 0, ksp_chain = kc->kc_chain;
98 (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
99 ksp_chain = ksp_chain->ks_next)
101 if (strncmp (ksp_chain->ks_class, "tape", 4) )
103 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
105 ksp[numtape++] = ksp_chain;
112 static void tape_submit (const char *plugin_instance,
114 counter_t read, counter_t write)
117 value_list_t vl = VALUE_LIST_INIT;
119 values[0].counter = read;
120 values[1].counter = write;
124 vl.time = time (NULL);
125 strcpy (vl.host, hostname_g);
126 strcpy (vl.plugin, "tape");
127 strncpy (vl.plugin_instance, plugin_instance,
128 sizeof (vl.plugin_instance));
130 plugin_dispatch_values (type, &vl);
131 } /* void tape_submit */
133 static int tape_read (void)
136 #if defined(HAVE_LIBKSTAT)
137 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
138 # define KIO_ROCTETS reads
139 # define KIO_WOCTETS writes
140 # define KIO_ROPS nreads
141 # define KIO_WOPS nwrites
142 # define KIO_RTIME rtime
143 # define KIO_WTIME wtime
144 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
145 # define KIO_ROCTETS nread
146 # define KIO_WOCTETS nwritten
147 # define KIO_ROPS reads
148 # define KIO_WOPS writes
149 # define KIO_RTIME rtime
150 # define KIO_WTIME wtime
152 # error "kstat_io_t does not have the required members"
154 static kstat_io_t kio;
163 for (i = 0; i < numtape; i++)
165 if (kstat_read (kc, ksp[i], &kio) == -1)
168 if (strncmp (ksp[i]->ks_class, "tape", 4) == 0)
170 tape_submit (ksp[i]->ks_name, "tape_octets",
171 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
172 tape_submit (ksp[i]->ks_name, "tape_ops",
173 kio.KIO_ROPS, kio.KIO_WOPS);
174 /* FIXME: Convert this to microseconds if necessary */
175 tape_submit (ksp[i]->ks_name, "tape_time",
176 kio.KIO_RTIME, kio.KIO_WTIME);
179 #endif /* defined(HAVE_LIBKSTAT) */
183 #endif /* TAPE_HAVE_READ */
185 void module_register (modreg_e load)
187 if (load & MR_DATASETS)
189 plugin_register_data_set (&octets_ds);
190 plugin_register_data_set (&operations_ds);
191 plugin_register_data_set (&merged_ds);
192 plugin_register_data_set (&time_ds);
198 plugin_register_init ("tape", tape_init);
199 plugin_register_read ("tape", tape_read);
201 #endif /* TAPE_HAVE_READ */