]> andersk Git - moira.git/blame - gen/hosts.dc
index hostalias
[moira.git] / gen / hosts.dc
CommitLineData
2289e07e 1/* $Header$
2 *
3 * This generates the hstath.txt hosttable.
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>
16EXEC SQL INCLUDE sqlca;
17
18extern int errno;
19char *whoami = "hosts.gen";
20
21
22main(argc, argv)
23int argc;
24char **argv;
25{
26 FILE *out = stdout;
27 char *outf = NULL, outft[64], *p, buf[256];
28 struct stat sb;
29 struct timeval now;
30 int flag1, flag2, i;
31 struct hash *aliases;
32 EXEC SQL BEGIN DECLARE SECTION;
33 int id;
34 char name[65], vendor[33], model[33], os[33], addr[17];
35 EXEC SQL END DECLARE SECTION;
36
37#ifsql INGRES
38 EXEC SQL CONNECT moira;
39 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
40#endsql
41#ifsql INFORMIX
42 EXEC SQL DATABASE moira;
43#endsql
44
45 if (argc == 2) {
46 if (stat(argv[1], &sb) == 0) {
47 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
48 ModDiff(&flag2, "subnet", sb.st_mtime))
49 exit(MR_DATE);
50 if (flag1 < 0 && flag2 < 0) {
51 fprintf(stderr, "File %s does not need to be rebuilt.\n",
52 argv[1]);
53 exit(MR_NO_CHANGE);
54 }
55 }
56 outf = argv[1];
57 sprintf(outft, "%s~", outf);
58 if ((out = fopen(outft, "w")) == NULL) {
59 fprintf(stderr, "unable to open %s for output\n", outf);
60 exit(MR_OCONFIG);
61 }
62 } else if (argc != 1) {
63 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
64 exit(MR_ARGS);
65 } else {
66 outf = NULL;
67 }
68
69 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
70
71 gettimeofday(&now, NULL);
72 fprintf(out, "; Host table generated %s;\n", ctime(&now.tv_sec));
73
74 EXEC SQL DECLARE y CURSOR FOR SELECT
75 mach_id, name FROM hostalias;
76 EXEC SQL OPEN y;
77 aliases = create_hash(1001);
78 while (1) {
79 EXEC SQL FETCH y INTO :id, :name;
80 if (sqlca.sqlcode != 0) break;
81 if (*strtrim(name) == 0) continue;
82 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
83 fprintf(stderr, "Name %s not in MIT domain\n", name);
84 continue;
85 } else {
86 name[i-8] = 0;
87 }
88 if (p = hash_lookup(aliases, id)) {
89 sprintf(buf, "%s,%s", p, name);
90 free(p);
91 hash_update(aliases, id, strsave(buf));
92 } else
93 hash_store(aliases, id, strsave(name));
94 }
95
96 EXEC SQL DECLARE x CURSOR FOR SELECT
97 name, mach_id, vendor, model, os, address
98 FROM machine WHERE status=1 ORDER BY address;
99 EXEC SQL OPEN x;
100 while (1) {
101 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
102 if (sqlca.sqlcode != 0) break;
103 if (*strtrim(name) == 0) continue;
104 if ((i = strlen(name)) < 9 || strcmp(&name[i-8], ".MIT.EDU")) {
105 fprintf(stderr, "Name %s not in MIT domain\n", name);
106 continue;
107 } else {
108 name[i-8] = 0;
109 }
110 strtrim(vendor);
111 strtrim(model);
112 strtrim(os);
113 strtrim(addr);
114 if (*addr == 0)
115 continue;
116 if (p = hash_lookup(aliases, id))
117 sprintf(buf, "%s,%s", name, p);
118 else
119 strcpy(buf, name);
120 fprintf(out, "HOST : %s : %s : ", addr, buf);
121 if (*vendor && *model)
122 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
123 else
124 fprintf(out, "%s%s : %s :\n", vendor, model, os);
125 }
126
127 EXEC SQL CLOSE x;
128#ifsql INGRES
129 EXEC SQL DISCONNECT;
130#endsql
131#ifsql INFORMIX
132 EXEC SQL CLOSE DATABASE;
133#endsql
134
135 fprintf(out, "; End of automatically generated host table\n");
136 if (fclose(out)) {
137 perror("close failed");
138 exit(MR_CCONFIG);
139 }
140 if (outf)
141 fix_file(outf);
142 exit(MR_SUCCESS);
143
144 sqlerr:
145 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
146 critical_alert("DCM", "Hosts build encountered INGRES ERROR %d",
147 sqlca.sqlcode);
148 exit(MR_INGRES_ERR);
149}
This page took 0.061652 seconds and 5 git commands to generate.