1 /* $Id: textfile.c,v 1.29 2011/09/08 18:26:08 jared Exp $ */
2 
3 #include "config.h"
4 
5 /*
6  * dump the currently broken network devices/services to the filename
7  * specified
8  *
9  * overwrite it if it exists, do not follow symlinks
10  * use the umask specified for file creation
11  * html = 0 (text) html = 1 (dump in html format)
12  *
13  */
14 void
dump_to_file(char * filename,int html,time_t now)15 dump_to_file(char *filename, int html, time_t now)
16 {
17 	FILE *fh; /* filehandle for writing out status */
18 
19 	char newfname[128], updated_at[128];
20 
21         struct tm *ltm;
22 
23         ltm = localtime(&now);
24         strftime(updated_at, 127,
25 	  parser_dateformat == NULL ? "%x %X" : parser_dateformat, ltm);
26 /*	snprintf(updated_at, 127,
27                 "%d/%d/%d @ %02d:%02d:%02d\n",
28                 ltm->tm_mon + 1,
29                 ltm->tm_mday,
30                 ltm->tm_year + 1900,
31                 ltm->tm_hour,
32                 ltm->tm_min,
33                 ltm->tm_sec); */
34 
35 
36 	snprintf(newfname, 127, "%s%d",filename, getpid());
37 
38 	if (filename == NULL)
39 	{
40 		print_err(1, "textfile.c:Error! dump_to_file called with filename == NULL\n");
41 		return;
42 	}
43 
44 	/* Delete anything that might be in our way */
45 	if(unlink(newfname) == -1)
46 	{
47 		if (debug)
48 		{
49 			print_err(1, "textfile.c:dump_to_file:unlink newfilename");
50 		}
51 	}
52 
53 	fh = fopen(newfname, "w");
54 
55 	if (fh == NULL)
56 	{
57 		if (debug)
58 			perror("dump_to_file:fopen");
59 		return;
60 	}
61 
62 	if (html) /* html format */
63 	{
64 		fprintf(fh, "<HTML>\n<HEAD>\n");
65 		fprintf(fh, "<TITLE>sysmon %s Network Summary</TITLE>\n", SYSM_VERS);
66                 fprintf(fh, "<META HTTP-EQUIV=\"Refresh\" CONTENT=%d>\n",
67 			parser_html_refresh);
68 		fprintf(fh, "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
69 		fprintf(fh, "<STYLE TYPE=\"text/css\" MEDIA=\"all\">\n");
70 		if (cssfilename) {
71 			fprintf(fh, "@import url(%s);\n", cssfilename);
72 		} else {
73 			fprintf(fh, "body {\n");
74 			fprintf(fh, "\tbackground-color: #ffffff;\n");
75 			fprintf(fh, "color: #000000;\n");
76 			fprintf(fh, "}\n");
77 			fprintf(fh, ".up {\n");
78 			fprintf(fh, "\tbackground-color: #%s;\n", upcolor);
79 			fprintf(fh, "\tcolor: #000000;\n");
80 			fprintf(fh, "}\n");
81 			fprintf(fh, ".down {\n");
82 			fprintf(fh, "\tbackground-color: #%s;\n", downcolor);
83 			fprintf(fh, "\tcolor: #000000;\n");
84 			fprintf(fh, "}\n");
85 			fprintf(fh, ".recent {\n");
86 			fprintf(fh, "\tbackground-color: #%s;\n", recentcolor);
87 			fprintf(fh, "\tcolor: #000000;\n");
88 			fprintf(fh, "}\n");
89 		}
90 		fprintf(fh, "</STYLE>\n");
91 		fprintf(fh, "</HEAD>\n");
92 		fprintf(fh, "<h2><a href=\"http://puck.nether.net/sysmon/\">sysmon</a> %s Network Summary</h2><p>\n", SYSM_VERS);
93 		if (paused)
94 		{
95 			fprintf(fh, "Monitoring disabled by user request<p>\n");
96 		}
97 		fprintf(fh, "\n<BODY BGCOLOR=\"#ffffff\">\n");
98 		fprintf(fh, "<TABLE BORDER=\"1\">\n");
99 		fprintf(fh, "<TR>\n");
100                 fprintf(fh,
101 			"<TD COLSPAN=12>Last Updated:<TT>%s</TT></TD>\n",
102 			updated_at);
103                 fprintf(fh, "<TR>\n");
104 		fprintf(fh, "<TD>HostName</TD>\n");
105 		fprintf(fh, "<TD>Description</td>\n");
106 		fprintf(fh, "<TD>Type</TD>\n");
107 		fprintf(fh, "<TD>Port</TD>\n");
108 		fprintf(fh, "<TD>Down N</TD>\n");
109 		fprintf(fh, "<TD>Up N</TD>\n");
110 		fprintf(fh, "<TD>Notified</TD>\n");
111 		fprintf(fh, "<TD>Status</TD>\n");
112 		fprintf(fh, "<TD>Time Up</TD>\n");
113 		fprintf(fh, "<TD>Time Failed</TD>\n");
114 		fprintf(fh, "<TD>Last Outage</TD>\n");
115 		fprintf(fh, "<TD>Uptime</TD>\n");
116 		fprintf(fh, "</TR>\n");
117 
118 	} else { /* non html */
119 		fprintf(fh,"Network Summary     sysmon %s\n", SYSM_VERS);
120 	        fprintf(fh,"%-25s%-6s%-5s%-6s%-6s%-6s%-15s%s\n",
121 			 "Hostname", "Type", "Port", "DownN", "UpN",
122 			"Notified", "Stat", "Time Failed");
123 	}
124 
125 	/* Insure the visited flag is not set */
126 	clear_visited();
127 
128 	/* print the hosts that are down and stuff */
129 	dump_to_file_walk_this_way(fh, configed_root, html, now);
130 
131 	if (html) /* html end of stuff */
132 	{
133 		fprintf(fh, "</TABLE>\n");
134 		fprintf(fh, "\n</BODY>\n");
135 		fprintf(fh, "\n</HTML>\n");
136 	} else {  /* ascii trailer */
137 		/* not used */
138 
139 	}
140 
141 	fclose(fh); /* close the file handle -- don't leak it */
142 
143 	chmod(newfname, 0444);
144 
145 	rename(newfname, filename);
146 
147 	return;
148 }
149 
add_line(FILE * fh,struct hostinfo down,int html,time_t now)150 void	add_line(FILE *fh, struct hostinfo down, int html, time_t now)
151 {
152         char *downdata = timedata(down.deathtime);
153         char *updata = timedata(down.last_up);
154         float value, tmp1, tmp2;
155         char tmp[1024];
156 
157 	if (html == 1)
158 	{
159 		if (down.lastcheck != SYSM_OK)
160 		{
161 			if (!down.contacted)
162 			{
163 			   fprintf(fh, "<TR class=\"recent\">\n");
164 			} else {
165 			   fprintf(fh, "<TR class=\"down\">\n");
166 			}
167 		} else {
168 			fprintf(fh, "<TR class=\"up\">\n");
169 		}
170                 tmp1 = down.totaldown;
171                 tmp2 = down.totalchecked;
172 		if (tmp2 != 0)
173 	                value = (100.0000-((tmp1/tmp2) * 100));
174 		else
175 			value = 100;
176                 if (value<0) value=0.000;
177                 snprintf(tmp, 1024, "%10.2f%%",value);
178 
179 		fprintf(fh,
180 "<TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%d</TD><TD>%ld</TD><TD>%ld</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD>\n",
181 			down.hostname, down.message, type_to_name(down.type),
182 			down.port, down.downct, down.upct,
183 			yes_no(down.contacted), errtostr(down.lastcheck),
184 			updata, downdata, str_difftime(down.last_up, now),tmp);
185 		fprintf(fh, "</TR>\n");
186 	} else { /* ascii */
187 		fprintf(fh,"%-25s%-6s%-5d%-5ld%-6ld%-6s%-15s%s\n",
188 		  down.hostname, type_to_name(down.type), down.port,
189 		  down.downct, down.upct, yes_no(down.contacted),
190 		  errtostr(down.lastcheck),
191 		  downdata);
192 	}
193 	FREE(downdata);
194 	FREE(updata);
195 }
196 
197 /*
198  * BUG: if parent is down, we display children
199  */
dump_to_file_walk_this_way(FILE * fh,struct graph_elements * here,int html,time_t now)200 void	dump_to_file_walk_this_way(FILE *fh, struct graph_elements *here,
201 		int html, time_t now)
202 {
203 	int x;
204 	int added = 0;
205 
206 	if (here == NULL)
207 		return;
208 	if (here->visit)
209 		return;
210         if (here->data == NULL)
211                 return;
212         /* Set the visited flag */
213         here->visit = TRUE;
214 
215 	/* Walk element list, display down hosts unless showupalso
216 	 * is set, then we show all hosts */
217 	/* BUG: Need ability to sort */
218 
219         if (here->data->lastcheck != 0 ||showupalso)
220         {
221 		add_line(fh, *(here->data), html, now);
222 		added = 1;
223 	}
224 	/* if showupalso OR not added (ie: down) */
225 	if (showupalso || (!added))
226 	{
227 		for (x = 0; x < here->tot_nei; x++)
228 		{
229 			dump_to_file_walk_this_way(fh, here->neighbors[x], html, now);
230 		}
231 	}
232 
233 }
234 
235