3 * This generates some useful reports.
5 * (c) Copyright 1993 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
13 #include <sys/types.h>
16 EXEC SQL INCLUDE sqlca;
19 char *whoami = "report.gen";
27 struct hash *users = NULL;
28 struct hash *kmaps = NULL;
34 char *outf = NULL, outft[64], *p, buf[256];
38 struct hash *aliases = NULL;
39 struct hash *nets = NULL;
42 EXEC SQL BEGIN DECLARE SECTION;
43 int id, sid, status, uid, ustatus, owner, creator, modby;
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;
51 EXEC SQL CONNECT moira;
52 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
55 EXEC SQL DATABASE moira;
60 if (stat(argv[1], &sb) == 0)
62 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
63 ModDiff(&flag2, "subnet", sb.st_mtime))
65 if (flag1 < 0 && flag2 < 0)
67 fprintf(stderr, "File %s does not need to be rebuilt.\n",
73 sprintf(outft, "%s~", outf);
74 if ((out = fopen(outft, "w")) == NULL)
76 fprintf(stderr, "unable to open %s for output\n", outf);
81 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
88 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
90 users = create_hash(12001);
96 EXEC SQL SELECT modtime INTO :name FROM users WHERE users_id = 0;
98 EXEC SQL DECLARE u_cursor CURSOR FOR
99 SELECT login, uid, users_id, status
101 EXEC SQL OPEN u_cursor;
105 EXEC SQL FETCH u_cursor INTO :login, :uid, :id, :ustatus;
106 if (sqlca.sqlcode != 0) break;
108 u = (struct user *) malloc(sizeof(struct user));
109 strcpy(u->name, login);
112 hash_store(users, id, u);
115 EXEC SQL CLOSE u_cursor;
118 * suck in host alias table
121 EXEC SQL SELECT modtime INTO :name FROM machine WHERE mach_id = 0;
123 gettimeofday(&now, NULL);
125 fprintf(out, "# Moira host table dump generated at %s\n",
128 EXEC SQL DECLARE y CURSOR FOR SELECT
129 mach_id, name FROM hostalias;
132 aliases = create_hash(1001);
135 EXEC SQL FETCH y INTO :id, :name;
137 if (sqlca.sqlcode != 0) break;
138 if (id == 0) continue;
139 if (p = hash_lookup(aliases, id))
141 sprintf(buf, "%s,%s", p, name);
142 hash_update(aliases, id, strsave(buf));
145 hash_store(aliases, id, strsave(name));
152 * suck in network table
155 EXEC SQL SELECT modtime INTO :name FROM subnet WHERE snet_id = 0;
157 EXEC SQL DECLARE n_cursor CURSOR FOR SELECT
158 snet_id, name FROM subnet;
159 EXEC SQL OPEN n_cursor;
161 nets = create_hash(301);
164 EXEC SQL FETCH n_cursor INTO :id, :name;
166 if (sqlca.sqlcode != 0) break;
167 if (id == 0) continue;
168 hash_store(nets, id, strsave(name));
171 EXEC SQL CLOSE n_cursor;
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
185 EXEC SQL FETCH x INTO :name, :id, :status, :schange, :vendor, :model, :os, :addr, :location, :sid, :owner_type, :owner, :modby, :modtime, :modwith, :created, :creator;
187 if (sqlca.sqlcode != 0) break;
188 if (id == 0) continue;
190 if (p = hash_lookup(aliases, id))
191 sprintf(buf, "%s,%s", name, p);
195 fprintf(out, "HOST|%s|", buf);
196 if(p = hash_lookup(nets, sid))
197 fprintf(out, "%s|", strtrim(p));
200 fprintf(out, "NONE|");
202 fprintf(out, "%d|", sid);
204 fprintf(out, "%s|%s|", strtrim(addr),
205 status == 3 ? "deleted" :
206 status == 2 ? "none" : status == 1 ? "active" : status == 0 ?
207 "reserved" : "unknown");
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));
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);
220 fprintf(out, "%s|", strtrim(modtime));
221 fprintf(out, "%s|", strtrim(modwith));
230 EXEC SQL CLOSE DATABASE;
233 fprintf(out, "# End of Report\n");
235 perror("close failed");
243 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
245 if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
248 critical_alert("DCM", "Report build encountered INGRES ERROR %d",
254 print_user(type, user)
260 EXEC SQL BEGIN DECLARE SECTION;
263 EXEC SQL END DECLARE SECTION;
265 if(strcmp(type, "USER") == 0)
267 if(u = (struct user *) hash_lookup(users, user))
268 fprintf(out, "USER %s|", u->name);
270 fprintf(out, "USER %d|", user);
274 fprintf(out, "smsuser|");
278 EXEC SQL SELECT string INTO :name FROM strings WHERE string_id = :id;
279 if (sqlca.sqlcode == 0)
280 fprintf(out, "%s|", strtrim(name));
282 fprintf(out, "UNKNOWN|");