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