This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaix_disk.c
177 lines (153 loc) · 6.03 KB
/
aix_disk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/******************************************************************************
*
* This program monitors the disk queues on AIX and posts the statistics to ganglia
* via gmetric binary
*
* Most of the code was provided by nagger:-
*
* http://www.ibm.com/developerworks/wikis/display/WikiPtype/ryo
*
* All I've added is the pid output/check, gmetric function and adjusted frequency
*
* Version 0.1: 21/07/2001
* - initial version
* - Metrics posted to ganglia every 15 seconds
*
*
*
* TODO - Look at converting to gmond c module - HELP Required!
*
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <libperfstat.h>
#include <sys/systemcfg.h>
static int already_running;
/* See /usr/include/sys/iplcb.h to explain the below */
#define XINTFRAC ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac))
/* hardware ticks per millisecond */
#define HWTICS2MSECS(x) ((double)x * XINTFRAC)/1000000.0
#define MAX_BUF_SIZE 1024
#ifndef FIRST_DISK
#define FIRST_DISK ""
#endif
call_gmetric_float(metric_name, metric_name_append, metric_value)
char *metric_name;
char *metric_name_append;
float metric_value;
{
char buf[MAX_BUF_SIZE];
char gmetric_command[1025];
int status;
FILE *f;
sprintf(gmetric_command, "/opt/freeware/bin/gmetric --name=%s_%s --value=%0.1f --type=int32 --dmax=60", metric_name, metric_name_append, metric_value);
/* printf("The following stats will be posted - %-16s\n",gmetric_command); */
f = popen( gmetric_command, "r");
status = pclose(f);
if (status > 0) {
/* Error reported by gmetric */
printf("gmetric reported and error\n");
}
}
char *fix(char *s)
{
int j;
for(j=0;j<IDENTIFIER_LENGTH;j++) {
if( !(isalpha(s[j]) ||
isdigit(s[j]) ||
s[j] == '-' ||
s[j] == '_' ||
s[j] == ' '
) ) {
s[j] = 0;
break;
}
}
return s;
}
int main(int argc, char* argv[])
{
int i, j, ret, disks;
perfstat_disk_t *a;
perfstat_disk_t *b;
perfstat_id_t name;
char *substring;
int running_pid;
int elapsed=15;
int ncpus;
pid_t pid, ppid;
gid_t gid;
FILE *fcheck, *pid_file;
/* find out if we are already running via pid file */
fcheck = fopen( "/var/run/gmond_iostats.pid", "r" );
if (fcheck)
{
already_running = 1;
fscanf(fcheck,"%d",&running_pid);
fclose( fcheck );
if (kill(running_pid, 0) == 0)
{
/* printf("The program is already running, pid %d\n", running_pid ); */
exit(0);
} else
printf("Stale pid file exists removing, please execute again\n");
remove("/var/run/gmond_iostats.pid");
exit(1);
} else
already_running = 0;
/* get the process id */
if ((pid = getpid()) < 0) {
perror("unable to get pid");
} else {
pid_file=fopen("/var/run/gmond_iostats.pid", "w+");
fprintf(pid_file, "%d", pid);
fclose(pid_file);
}
/* get the number of CPUs */
ncpus=perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
/* check how many perfstat_disk_t structures are available */
disks = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0);
if(disks==-1) {
perror("perfstat_disk(NULL)");
exit(3);
}
/* allocate enough memory for all the structures */
a = malloc( sizeof(perfstat_disk_t) * disks);
b = malloc( sizeof(perfstat_disk_t) * disks);
/* ask to get all the structures available in one call */
/* return code is number of structures returned */
strcpy(name.name,FIRST_DISK);
ret = perfstat_disk(&name, a, sizeof(perfstat_disk_t), disks);
if(disks==-1) {
perror("perfstat_disk(a)");
exit(4);
}
for(;;) {
#define DELTA(member) (b[i].member - a[i].member)
sleep(elapsed);
strcpy(name.name,FIRST_DISK);
ret = perfstat_disk(&name, b, sizeof(perfstat_disk_t), disks);
if(ret==-1) {
perror("perfstat_disk(b)");
exit(4);
}
for (i = 0; i < ret; i++) {
#define NONZERO(x) ((x)?(x):1)
call_gmetric_float(fix(b[i].name), "avgtime", (double)(HWTICS2MSECS(DELTA(wq_time))/NONZERO(DELTA(xfers))/elapsed));
call_gmetric_float(fix(b[i].name), "avgWQsz", (double)(DELTA(wq_sampled))/(100.0*(double)elapsed*(double)ncpus));
call_gmetric_float(fix(b[i].name), "avgSQsz", (double)(DELTA(q_sampled))/(100.0*(double)elapsed*(double)ncpus));
call_gmetric_float(fix(b[i].name), "SQfull", DELTA(q_full));
/* Enabled for debugging
printf("%-16s avgtime avgWQsz avgSQsz SQfull\n",fix(b[i].name));
printf(" %8.1f %7.1f %7.1f %3llu \n",
(double)(HWTICS2MSECS(DELTA(wq_time))/NONZERO(DELTA(xfers))/elapsed),
(double)(DELTA(wq_sampled))/(100.0*(double)elapsed*(double)ncpus),
(double)(DELTA(q_sampled))/(100.0*(double)elapsed*(double)ncpus),
DELTA(q_full)); */
}
memcpy(a,b,sizeof(perfstat_disk_t) * disks );
}
}