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
|
/*
* Linux Scsi Devices Cluster
*
* Copyright (c) 2014 Red Hat.
* Copyright (c) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include "indom.h"
#include "proc_scsi.h"
static char diskname[64];
static char tapename[64];
static char cdromname[64];
int
refresh_proc_scsi(proc_scsi_t *scsi)
{
char buf[1024];
char name[1024];
int i;
int n;
FILE *fp;
char *sp;
static int have_devfs = -1;
static int next_id = -1;
if (next_id < 0) {
/* one trip initialization */
next_id = 0;
scsi->nscsi = 0;
scsi->scsi = (scsi_entry_t *)malloc(sizeof(scsi_entry_t));
/* scsi indom */
scsi->scsi_indom->it_numinst = 0;
scsi->scsi_indom->it_set = (pmdaInstid *)malloc(sizeof(pmdaInstid));
/* devfs naming convention */
have_devfs = access("/dev/.devfsd", F_OK) == 0;
if (have_devfs) {
strcpy(diskname, "scsi/host%d/bus%d/target%d/lun%d/disc");
strcpy(tapename, "st0");
strcpy(cdromname, "scd0");
}
else {
strcpy(diskname, "sda");
strcpy(tapename, "st0");
strcpy(cdromname, "scd0");
}
}
if ((fp = linux_statsfile("/proc/scsi/scsi", buf, sizeof(buf))) == NULL)
return -oserror();
while (fgets(buf, sizeof(buf), fp) != NULL) {
scsi_entry_t x = { 0 };
if (strncmp(buf, "Host:", 5) != 0)
continue;
n = sscanf(buf, "Host: scsi%d Channel: %d Id: %d Lun: %d",
&x.dev_host, &x.dev_channel, &x.dev_id, &x.dev_lun);
if (n != 4)
continue;
for (i=0; i < scsi->nscsi; i++) {
if (scsi->scsi[i].dev_host == x.dev_host &&
scsi->scsi[i].dev_channel == x.dev_channel &&
scsi->scsi[i].dev_id == x.dev_id &&
scsi->scsi[i].dev_lun == x.dev_lun)
break;
}
if (i == scsi->nscsi) {
scsi->nscsi++;
scsi->scsi = (scsi_entry_t *)realloc(scsi->scsi,
scsi->nscsi * sizeof(scsi_entry_t));
memcpy(&scsi->scsi[i], &x, sizeof(scsi_entry_t));
scsi->scsi[i].id = next_id++;
/* scan for the Vendor: and Type: strings */
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((sp = strstr(buf, "Type:")) != (char *)NULL) {
if (sscanf(sp, "Type: %s", name) == 1)
scsi->scsi[i].dev_type = strdup(name);
else
scsi->scsi[i].dev_type = strdup("unknown");
break;
}
}
if (strcmp(scsi->scsi[i].dev_type, "Direct-Access") == 0) {
if (have_devfs) {
scsi->scsi[i].dev_name = (char *)malloc(64);
sprintf(scsi->scsi[i].dev_name, diskname,
scsi->scsi[i].dev_host, scsi->scsi[i].dev_channel,
scsi->scsi[i].dev_id, scsi->scsi[i].dev_lun);
}
else {
scsi->scsi[i].dev_name = strdup(diskname);
diskname[2]++; /* sd[a-z] bump to next disk device name */
}
}
else
if (strcmp(scsi->scsi[i].dev_type, "Sequential-Access") == 0) {
scsi->scsi[i].dev_name = strdup(tapename);
tapename[2]++; /* st[0-9] bump to next tape device name */
}
else
if (strcmp(scsi->scsi[i].dev_type, "CD-ROM") == 0) {
scsi->scsi[i].dev_name = strdup(cdromname);
cdromname[3]++; /* scd[0-9] bump to next CDROM device name */
}
else
if (strcmp(scsi->scsi[i].dev_type, "Processor") == 0)
scsi->scsi[i].dev_name = strdup("SCSI Controller");
else
scsi->scsi[i].dev_name = strdup("Unknown SCSI device");
sprintf(name, "scsi%d:%d:%d:%d %s", scsi->scsi[i].dev_host,
scsi->scsi[i].dev_channel, scsi->scsi[i].dev_id, scsi->scsi[i].dev_lun, scsi->scsi[i].dev_type);
scsi->scsi[i].namebuf = strdup(name);
#if PCP_DEBUG
if (pmDebug & DBG_TRACE_LIBPMDA) {
fprintf(stderr, "refresh_proc_scsi: add host=scsi%d channel=%d id=%d lun=%d type=%s\n",
scsi->scsi[i].dev_host, scsi->scsi[i].dev_channel,
scsi->scsi[i].dev_id, scsi->scsi[i].dev_lun,
scsi->scsi[i].dev_type);
}
#endif
}
}
/* refresh scsi indom */
if (scsi->scsi_indom->it_numinst != scsi->nscsi) {
scsi->scsi_indom->it_numinst = scsi->nscsi;
scsi->scsi_indom->it_set = (pmdaInstid *)realloc(scsi->scsi_indom->it_set,
scsi->nscsi * sizeof(pmdaInstid));
memset(scsi->scsi_indom->it_set, 0, scsi->nscsi * sizeof(pmdaInstid));
}
for (i=0; i < scsi->nscsi; i++) {
scsi->scsi_indom->it_set[i].i_inst = scsi->scsi[i].id;
scsi->scsi_indom->it_set[i].i_name = scsi->scsi[i].namebuf;
}
fclose(fp);
return 0;
}
|