Bumped version to 3.11.5; Updated ChangeLog.
[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 #define MODULE_NAME "tape"
28
29 #if defined(HAVE_LIBKSTAT)
30 # define TAPE_HAVE_READ 1
31 #else
32 # define TAPE_HAVE_READ 0
33 #endif
34
35 static char *tape_filename_template = "tape-%s.rrd";
36
37 /* 104857600 == 100 MB */
38 static char *tape_ds_def[] =
39 {
40         "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
41         "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
42         "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:U",
43         "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
44         "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
45         "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
46         "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:U",
47         "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
48         NULL
49 };
50 static int tape_ds_num = 8;
51
52 #if defined(HAVE_LIBKSTAT)
53 #define MAX_NUMTAPE 256
54 extern kstat_ctl_t *kc;
55 static kstat_t *ksp[MAX_NUMTAPE];
56 static int numtape = 0;
57 #endif /* HAVE_LIBKSTAT */
58
59 static void tape_init (void)
60 {
61 #ifdef HAVE_LIBKSTAT
62         kstat_t *ksp_chain;
63
64         numtape = 0;
65
66         if (kc == NULL)
67                 return;
68
69         for (numtape = 0, ksp_chain = kc->kc_chain;
70                         (numtape < MAX_NUMTAPE) && (ksp_chain != NULL);
71                         ksp_chain = ksp_chain->ks_next)
72         {
73                 if (strncmp (ksp_chain->ks_class, "tape", 4) )
74                         continue;
75                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
76                         continue;
77                 ksp[numtape++] = ksp_chain;
78         }
79 #endif
80
81         return;
82 }
83
84 static void tape_write (char *host, char *inst, char *val)
85 {
86         char file[512];
87         int status;
88
89         status = snprintf (file, 512, tape_filename_template, inst);
90         if (status < 1)
91                 return;
92         else if (status >= 512)
93                 return;
94
95         rrd_update_file (host, file, val, tape_ds_def, tape_ds_num);
96 }
97
98
99 #if TAPE_HAVE_READ
100 #define BUFSIZE 512
101 static void tape_submit (char *tape_name,
102                 unsigned long long read_count,
103                 unsigned long long read_merged,
104                 unsigned long long read_bytes,
105                 unsigned long long read_time,
106                 unsigned long long write_count,
107                 unsigned long long write_merged,
108                 unsigned long long write_bytes,
109                 unsigned long long write_time)
110
111 {
112         char buf[BUFSIZE];
113
114         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
115                                 (unsigned int) curtime,
116                                 read_count, read_merged, read_bytes, read_time,
117                                 write_count, write_merged, write_bytes,
118                                 write_time) >= BUFSIZE)
119                 return;
120
121         plugin_submit (MODULE_NAME, tape_name, buf);
122 }
123
124 #undef BUFSIZE
125
126 static void tape_read (void)
127 {
128
129 #if defined(HAVE_LIBKSTAT)
130         static kstat_io_t kio;
131         int i;
132
133         if (kc == NULL)
134                 return;
135
136         for (i = 0; i < numtape; i++)
137         {
138                 if (kstat_read (kc, ksp[i], &kio) == -1)
139                         continue;
140
141                 if (strncmp (ksp[i]->ks_class, "tape", 4) == 0)
142                         tape_submit (ksp[i]->ks_name,
143                                         kio.reads,  0LL, kio.nread,    kio.rtime,
144                                         kio.writes, 0LL, kio.nwritten, kio.wtime);
145         }
146 #endif /* defined(HAVE_LIBKSTAT) */
147 }
148 #else
149 # define tape_read NULL
150 #endif /* TAPE_HAVE_READ */
151
152 void module_register (void)
153 {
154         plugin_register (MODULE_NAME, tape_init, tape_read, tape_write);
155 }
156
157 #undef MODULE_NAME