]>
Commit | Line | Data |
---|---|---|
3d8d4b36 | 1 | /* $Header$ |
2 | * | |
3 | * This generates some useful reports. | |
4 | * | |
5 | * (c) Copyright 1993 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <stdio.h> | |
12 | #include <moira.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/time.h> | |
16 | EXEC SQL INCLUDE sqlca; | |
17 | ||
18 | extern int errno; | |
19 | char *whoami = "report.gen"; | |
20 | FILE *out = stdout; | |
21 | ||
22 | struct user { | |
23 | char name[9]; | |
24 | int uid; | |
25 | int status; | |
26 | }; | |
27 | struct hash *users = NULL; | |
28 | struct hash *kmaps = NULL; | |
29 | ||
30 | main(argc, argv) | |
31 | int argc; | |
32 | char **argv; | |
33 | { | |
34 | char *outf = NULL, outft[64], *p, buf[256]; | |
35 | struct stat sb; | |
36 | struct timeval now; | |
37 | int flag1, flag2, i; | |
38 | struct hash *aliases = NULL; | |
39 | struct hash *nets = NULL; | |
40 | struct user *u; | |
41 | ||
42 | EXEC SQL BEGIN DECLARE SECTION; | |
43 | int id, sid, status, uid, ustatus, owner, creator, modby; | |
44 | char login[9]; | |
45 | char name[65], vendor[33], model[33], os[33], addr[17]; | |
46 | char location[17], owner_type[16]; | |
47 | char schange[25], modtime[25], modwith[33], created[25]; | |
48 | EXEC SQL END DECLARE SECTION; | |
49 | ||
50 | #ifsql INGRES | |
51 | EXEC SQL CONNECT moira; | |
52 | EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED; | |
53 | #endsql | |
54 | #ifsql INFORMIX | |
55 | EXEC SQL DATABASE moira; | |
56 | #endsql | |
57 | ||
58 | if (argc == 2) | |
59 | { | |
60 | if (stat(argv[1], &sb) == 0) | |
61 | { | |
62 | if (ModDiff(&flag1, "machine", sb.st_mtime) || | |
63 | ModDiff(&flag2, "subnet", sb.st_mtime)) | |
64 | exit(MR_DATE); | |
65 | if (flag1 < 0 && flag2 < 0) | |
66 | { | |
67 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
68 | argv[1]); | |
69 | exit(MR_NO_CHANGE); | |
70 | } | |
71 | } | |
72 | outf = argv[1]; | |
73 | sprintf(outft, "%s~", outf); | |
74 | if ((out = fopen(outft, "w")) == NULL) | |
75 | { | |
76 | fprintf(stderr, "unable to open %s for output\n", outf); | |
77 | exit(MR_OCONFIG); | |
78 | } | |
79 | } else if (argc != 1) | |
80 | { | |
81 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
82 | exit(MR_ARGS); | |
83 | } else | |
84 | { | |
85 | outf = NULL; | |
86 | } | |
87 | ||
88 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
89 | ||
90 | users = create_hash(12001); | |
91 | ||
92 | /* | |
93 | * suck in user table | |
94 | */ | |
95 | ||
96 | EXEC SQL SELECT modtime INTO :name FROM users WHERE users_id = 0; | |
97 | ||
98 | EXEC SQL DECLARE u_cursor CURSOR FOR | |
99 | SELECT login, uid, users_id, status | |
100 | FROM users; | |
101 | EXEC SQL OPEN u_cursor; | |
102 | ||
103 | while (1) | |
104 | { | |
105 | EXEC SQL FETCH u_cursor INTO :login, :uid, :id, :ustatus; | |
106 | if (sqlca.sqlcode != 0) break; | |
107 | strtrim(login); | |
108 | u = (struct user *) malloc(sizeof(struct user)); | |
109 | strcpy(u->name, login); | |
110 | u->uid = uid; | |
111 | u->status = ustatus; | |
112 | hash_store(users, id, u); | |
113 | } | |
114 | ||
115 | EXEC SQL CLOSE u_cursor; | |
116 | ||
117 | /* | |
118 | * suck in host alias table | |
119 | */ | |
120 | ||
121 | EXEC SQL SELECT modtime INTO :name FROM machine WHERE mach_id = 0; | |
122 | ||
123 | gettimeofday(&now, NULL); | |
124 | ||
125 | fprintf(out, "# Moira host table dump generated at %s\n", | |
126 | ctime(&now.tv_sec)); | |
127 | ||
128 | EXEC SQL DECLARE y CURSOR FOR SELECT | |
129 | mach_id, name FROM hostalias; | |
130 | EXEC SQL OPEN y; | |
131 | ||
132 | aliases = create_hash(1001); | |
133 | while (1) | |
134 | { | |
135 | EXEC SQL FETCH y INTO :id, :name; | |
136 | ||
137 | if (sqlca.sqlcode != 0) break; | |
138 | if (id == 0) continue; | |
139 | if (p = hash_lookup(aliases, id)) | |
140 | { | |
141 | sprintf(buf, "%s,%s", p, name); | |
142 | hash_update(aliases, id, strsave(buf)); | |
143 | free(p); | |
144 | } else | |
145 | hash_store(aliases, id, strsave(name)); | |
146 | } | |
147 | ||
148 | EXEC SQL CLOSE y; | |
149 | ||
150 | ||
151 | /* | |
152 | * suck in network table | |
153 | */ | |
154 | ||
155 | EXEC SQL SELECT modtime INTO :name FROM subnet WHERE snet_id = 0; | |
156 | ||
157 | EXEC SQL DECLARE n_cursor CURSOR FOR SELECT | |
158 | snet_id, name FROM subnet; | |
159 | EXEC SQL OPEN n_cursor; | |
160 | ||
161 | nets = create_hash(301); | |
162 | while (1) | |
163 | { | |
164 | EXEC SQL FETCH n_cursor INTO :id, :name; | |
165 | ||
166 | if (sqlca.sqlcode != 0) break; | |
167 | if (id == 0) continue; | |
168 | hash_store(nets, id, strsave(name)); | |
169 | } | |
170 | ||
171 | EXEC SQL CLOSE n_cursor; | |
172 | ||
173 | /* | |
174 | * dump machine table | |
175 | */ | |
176 | ||
177 | ||
178 | EXEC SQL DECLARE x CURSOR FOR SELECT | |
179 | name, mach_id, status, statuschange, vendor, model, os, address, location, snet_id, owner_type, owner_id, modby, modtime, modwith, created, creator | |
180 | FROM machine; | |
181 | EXEC SQL OPEN x; | |
182 | ||
183 | while (1) | |
184 | { | |
185 | EXEC SQL FETCH x INTO :name, :id, :status, :schange, :vendor, :model, :os, :addr, :location, :sid, :owner_type, :owner, :modby, :modtime, :modwith, :created, :creator; | |
186 | ||
187 | if (sqlca.sqlcode != 0) break; | |
188 | if (id == 0) continue; | |
189 | ||
190 | if (p = hash_lookup(aliases, id)) | |
191 | sprintf(buf, "%s,%s", name, p); | |
192 | else | |
193 | strcpy(buf, name); | |
194 | ||
195 | fprintf(out, "HOST|%s|", buf); | |
196 | if(p = hash_lookup(nets, sid)) | |
197 | fprintf(out, "%s|", strtrim(p)); | |
198 | else | |
199 | if(sid == 0) | |
200 | fprintf(out, "NONE|"); | |
201 | else | |
202 | fprintf(out, "%d|", sid); | |
203 | ||
204 | fprintf(out, "%s|%s|", strtrim(addr), | |
205 | status == 3 ? "deleted" : | |
206 | status == 2 ? "none" : status == 1 ? "active" : status == 0 ? | |
207 | "reserved" : "unknown"); | |
208 | ||
209 | fprintf(out, "%s|", strtrim(schange)); | |
210 | fprintf(out, "%s|", strtrim(location)); | |
211 | fprintf(out, "%s|", strtrim(vendor)); | |
212 | fprintf(out, "%s|", strtrim(model)); | |
213 | fprintf(out, "%s|", strtrim(os)); | |
214 | ||
215 | print_user(strtrim(owner_type), owner); | |
216 | print_user("STRING", creator); | |
217 | fprintf(out, "%s|", strtrim(created)); | |
218 | print_user("STRING", modby < 0 ? -modby : modby); | |
219 | ||
220 | fprintf(out, "%s|", strtrim(modtime)); | |
221 | fprintf(out, "%s|", strtrim(modwith)); | |
222 | fprintf(out, "\n"); | |
223 | } | |
224 | ||
225 | EXEC SQL CLOSE x; | |
226 | #ifsql INGRES | |
227 | EXEC SQL DISCONNECT; | |
228 | #endsql | |
229 | #ifsql INFORMIX | |
230 | EXEC SQL CLOSE DATABASE; | |
231 | #endsql | |
232 | ||
233 | fprintf(out, "# End of Report\n"); | |
234 | if (fclose(out)) { | |
235 | perror("close failed"); | |
236 | exit(MR_CCONFIG); | |
237 | } | |
238 | if (outf) | |
239 | fix_file(outf); | |
240 | exit(MR_SUCCESS); | |
241 | ||
242 | sqlerr: | |
243 | com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode); | |
244 | #ifsql INGRES | |
245 | if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000) | |
246 | exit(MR_DEADLOCK); | |
247 | #endsql | |
248 | critical_alert("DCM", "Report build encountered INGRES ERROR %d", | |
249 | sqlca.sqlcode); | |
250 | exit(MR_INGRES_ERR); | |
251 | } | |
252 | ||
253 | ||
254 | print_user(type, user) | |
255 | char *type; | |
256 | unsigned int user; | |
257 | { | |
258 | struct user *u; | |
259 | char *c; | |
260 | EXEC SQL BEGIN DECLARE SECTION; | |
261 | int id; | |
262 | char name[128]; | |
263 | EXEC SQL END DECLARE SECTION; | |
264 | ||
265 | if(strcmp(type, "USER") == 0) | |
266 | { | |
267 | if(u = (struct user *) hash_lookup(users, user)) | |
268 | fprintf(out, "USER %s|", u->name); | |
269 | else | |
270 | fprintf(out, "USER %d|", user); | |
271 | } | |
272 | else | |
273 | if(id == 0) | |
274 | fprintf(out, "smsuser|"); | |
275 | else | |
276 | { | |
277 | id = user; | |
278 | EXEC SQL SELECT string INTO :name FROM strings WHERE string_id = :id; | |
279 | if (sqlca.sqlcode == 0) | |
280 | fprintf(out, "%s|", strtrim(name)); | |
281 | else | |
282 | fprintf(out, "UNKNOWN|"); | |
283 | } | |
284 | sqlerr: | |
285 | return; | |
286 | } |