]> andersk Git - moira.git/blame - gen/hosts.pc
Use moira_schema.h
[moira.git] / gen / hosts.pc
CommitLineData
7ac48069 1/* $Id$
2289e07e 2 *
3 * This generates the hstath.txt hosttable.
4 *
7ac48069 5 * (c) Copyright 1993-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
2289e07e 8 */
9
10#include <mit-copyright.h>
2289e07e 11#include <moira.h>
7ac48069 12
2289e07e 13#include <sys/stat.h>
7ac48069 14
15#include <netinet/in.h>
16#include <arpa/inet.h>
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
23#include "util.h"
24
2289e07e 25EXEC SQL INCLUDE sqlca;
26
7ac48069 27RCSID("$Header$");
28
2289e07e 29char *whoami = "hosts.gen";
9c04b191 30char *db = "moira/moira";
2289e07e 31
5eaef520 32int main(int argc, char **argv)
2289e07e 33{
5eaef520 34 FILE *out = stdout;
35 char *outf = NULL, outft[64], *p, buf[256];
36 struct stat sb;
37 struct timeval now;
38 int flag1, flag2, i;
39 struct hash *aliases;
40 EXEC SQL BEGIN DECLARE SECTION;
41 int id;
42 char name[65], vendor[33], model[33], os[33], addr[17];
43 EXEC SQL END DECLARE SECTION;
2289e07e 44
5eaef520 45 EXEC SQL CONNECT :db;
2289e07e 46
5eaef520 47 if (argc == 2)
48 {
49 if (stat(argv[1], &sb) == 0)
50 {
51 if (ModDiff(&flag1, "machine", sb.st_mtime) ||
52 ModDiff(&flag2, "subnet", sb.st_mtime))
53 exit(MR_DATE);
54 if (flag1 < 0 && flag2 < 0)
55 {
56 fprintf(stderr, "File %s does not need to be rebuilt.\n",
57 argv[1]);
58 exit(MR_NO_CHANGE);
2289e07e 59 }
60 }
5eaef520 61 outf = argv[1];
62 sprintf(outft, "%s~", outf);
63 if (!(out = fopen(outft, "w")))
64 {
65 fprintf(stderr, "unable to open %s for output\n", outf);
66 exit(MR_OCONFIG);
2289e07e 67 }
2289e07e 68 }
5eaef520 69 else if (argc != 1)
70 {
71 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
72 exit(MR_ARGS);
73 }
74 else
75 outf = NULL;
76
77 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
2289e07e 78
5eaef520 79 gettimeofday(&now, NULL);
2289e07e 80
5eaef520 81 fprintf(out, "; MIT Network Host Table\n;\n");
82 fprintf(out, "; \t%cAuthor: $\n", '$');
83 fprintf(out, "; \t%cDate: $\n", '$');
84 fprintf(out, "; \t%cRevision: $\n;\n", '$');
85 fprintf(out, "; Host table generated by Moira at %s;\n",
86 ctime(&now.tv_sec));
2289e07e 87
5eaef520 88 EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias;
89 EXEC SQL OPEN y;
90 aliases = create_hash(1001);
91 while (1)
92 {
93 EXEC SQL FETCH y INTO :id, :name;
94 if (sqlca.sqlcode)
95 break;
96 if (id == 0)
97 continue;
98 if (!*strtrim(name))
99 continue;
ed7faf49 100 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
101 strcmp(strchr(name, '.'), ".MIT.EDU"))
5eaef520 102 {
ed7faf49 103 fprintf(stderr, "Ignoring alias %s\n", name);
5eaef520 104 continue;
2289e07e 105 }
5eaef520 106 else
107 name[i - 8] = 0;
7ac48069 108 if ((p = hash_lookup(aliases, id)))
5eaef520 109 {
110 sprintf(buf, "%s,%s", p, name);
7ac48069 111 hash_update(aliases, id, strdup(buf));
5eaef520 112 free(p);
113 }
114 else
7ac48069 115 hash_store(aliases, id, strdup(name));
2289e07e 116 }
117
5eaef520 118 EXEC SQL DECLARE x CURSOR FOR SELECT
119 name, mach_id, vendor, model, os, address
120 FROM machine WHERE status = 1 ORDER BY address;
121 EXEC SQL OPEN x;
122 while (1)
123 {
124 EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr;
125 if (sqlca.sqlcode)
126 break;
127 if (id == 0)
128 continue;
129 if (!*strtrim(name))
130 continue;
ed7faf49 131 if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
132 strcmp(strchr(name, '.'), ".MIT.EDU"))
133 {
134 fprintf(stderr, "Ignoring machine %s\n", name);
135 continue;
136 }
5eaef520 137 else
138 name[i - 8] = 0;
139 strtrim(vendor);
140 strtrim(model);
141 strtrim(os);
142 strtrim(addr);
143 if (*addr == 0 || inet_addr(addr) == -1)
144 continue;
145 if ((p = hash_lookup(aliases, id)))
146 sprintf(buf, "%s,%s", name, p);
147 else
148 strcpy(buf, name);
149 fprintf(out, "HOST : %s : %s : ", addr, buf);
150 if ((*vendor || *model) && *os)
151 {
152 if (*vendor && *model)
153 fprintf(out, "%s/%s : %s :\n", vendor, model, os);
154 else
155 fprintf(out, "%s%s : %s :\n", vendor, model, os);
2289e07e 156 }
5eaef520 157 else
158 fputs("\n", out);
2289e07e 159 }
160
5eaef520 161 EXEC SQL CLOSE x;
9c04b191 162
5eaef520 163 EXEC SQL COMMIT;
2289e07e 164
5eaef520 165 fprintf(out, "; End of automatically generated host table\n");
166 if (fclose(out))
167 {
168 perror("close failed");
169 exit(MR_CCONFIG);
2289e07e 170 }
5eaef520 171 if (outf)
172 fix_file(outf);
173 exit(MR_SUCCESS);
2289e07e 174
5eaef520 175sqlerr:
176 db_error(sqlca.sqlcode);
177 exit(MR_DBMS_ERR);
2289e07e 178}
This page took 0.125661 seconds and 5 git commands to generate.