1 /*----------------------------------------------------------------------------*/
2 /* Xymon message daemon.                                                      */
3 /*                                                                            */
4 /* Client backend module for FreeBSD                                          */
5 /*                                                                            */
6 /* Copyright (C) 2005-2011 Henrik Storner <henrik@hswn.dk>                    */
7 /*                                                                            */
8 /* This program is released under the GNU General Public License (GPL),       */
9 /* version 2. See the file "COPYING" for details.                             */
10 /*                                                                            */
11 /*----------------------------------------------------------------------------*/
12 
13 static char freebsd_rcsid[] = "$Id: freebsd.c 7886 2016-02-02 20:16:19Z jccleaver $";
14 
handle_freebsd_client(char * hostname,char * clienttype,enum ostype_t os,void * hinfo,char * sender,time_t timestamp,char * clientdata)15 void handle_freebsd_client(char *hostname, char *clienttype, enum ostype_t os,
16 			   void *hinfo, char *sender, time_t timestamp,
17 			   char *clientdata)
18 {
19 	char *timestr;
20 	char *uptimestr;
21 	char *clockstr;
22 	char *msgcachestr;
23 	char *whostr;
24 	char *psstr;
25 	char *topstr;
26 	char *dfstr;
27 	char *inodestr;
28 	char *meminfostr;
29 	char *swapinfostr;
30 	char *vmtotalstr;
31 	char *msgsstr;
32 	char *netstatstr;
33 	char *ifstatstr;
34 	char *portsstr;
35 	char *vmstatstr;
36 
37 	char *p;
38 	char fromline[1024];
39 
40 	unsigned long memphystotal = 0, memphysfree = 0, memphysused = 0, memphysactual = -1;
41 	unsigned long memswaptotal = 0, memswapfree = 0, memswapused = 0;
42 	int found = 0;
43 
44 	sprintf(fromline, "\nStatus message received from %s\n", sender);
45 
46 	splitmsg(clientdata);
47 
48 	timestr = getdata("date");
49 	uptimestr = getdata("uptime");
50 	clockstr = getdata("clock");
51 	msgcachestr = getdata("msgcache");
52 	whostr = getdata("who");
53 	psstr = getdata("ps");
54 	topstr = getdata("top");
55 	dfstr = getdata("df");
56 	inodestr = getdata("inode");
57 	meminfostr = getdata("meminfo");
58 	swapinfostr = getdata("swapinfo");
59 	msgsstr = getdata("msgs");
60 	netstatstr = getdata("netstat");
61 	ifstatstr = getdata("ifstat");
62 	portsstr = getdata("ports");
63 	vmstatstr = getdata("vmstat");
64 	vmtotalstr = getdata("vmtotal");
65 
66 	unix_cpu_report(hostname, clienttype, os, hinfo, fromline, timestr, uptimestr, clockstr, msgcachestr,
67 			whostr, 0, psstr, 0, topstr);
68 	unix_disk_report(hostname, clienttype, os, hinfo, fromline, timestr, "Avail", "Capacity", "Mounted", dfstr);
69 	unix_inode_report(hostname, clienttype, os, hinfo, fromline, timestr, "ifree", "%iused", "Mounted", inodestr);
70 	unix_procs_report(hostname, clienttype, os, hinfo, fromline, timestr, "COMMAND", NULL, psstr);
71 	unix_ports_report(hostname, clienttype, os, hinfo, fromline, timestr, 3, 4, 5, portsstr);
72 
73 	msgs_report(hostname, clienttype, os, hinfo, fromline, timestr, msgsstr);
74 	file_report(hostname, clienttype, os, hinfo, fromline, timestr);
75 	linecount_report(hostname, clienttype, os, hinfo, fromline, timestr);
76 	deltacount_report(hostname, clienttype, os, hinfo, fromline, timestr);
77 
78 	unix_netstat_report(hostname, clienttype, os, hinfo, fromline, timestr, netstatstr);
79 	unix_ifstat_report(hostname, clienttype, os, hinfo, fromline, timestr, ifstatstr);
80 	unix_vmstat_report(hostname, clienttype, os, hinfo, fromline, timestr, vmstatstr);
81 
82 	if (meminfostr) {
83 		p = strstr(meminfostr, "Total:"); if (p) { memphystotal = atol(p+6); found++; }
84 		p = strstr(meminfostr, "Actual:"); if (p) memphysactual = atol(p+7);
85 	}
86 
87 	if (vmtotalstr) {
88 		p = strstr(vmtotalstr, "\nFree Memory Pages:");
89 		if (p) {
90 			memphysfree = atol(p + 19)/1024;
91 			memphysused = memphystotal - memphysfree;
92 			found++;
93 		} else {
94 			p = strstr(vmtotalstr, "\nFree Memory:");
95 			if (p) {
96 				memphysfree = atol(p + 13)/1024;
97 				memphysused = memphystotal - memphysfree;
98 				found++;
99 			}
100 		}
101 	}
102 
103 	if ((found == 1) && meminfostr) {
104 		p = strstr(meminfostr, "Free:");  if (p) { memphysfree  = atol(p+5); found++; }
105 		memphysused = memphystotal - memphysfree;
106 	}
107 
108 	if (swapinfostr) {
109 		found++;
110 		p = strchr(swapinfostr, '\n'); /* Skip the header line */
111 		while (p) {
112 			long stot, sused, sfree;
113 			char *bol;
114 
115 			bol = p+1;
116 			p = strchr(bol, '\n'); if (p) *p = '\0';
117 
118 			if (sscanf(bol, "%*s %ld %ld %ld", &stot, &sused, &sfree) == 3) {
119 				memswaptotal += stot;
120 				memswapused += sused;
121 				memswapfree += sfree;
122 			}
123 
124 			if (p) *p = '\n';
125 		}
126 
127 		memswaptotal /= 1024; memswapused /= 1024; memswapfree /= 1024;
128 	}
129 
130 	if (found >= 2) {
131 		unix_memory_report(hostname, clienttype, os, hinfo, fromline, timestr,
132 			   memphystotal, memphysused, memphystotal, memphysactual, memswaptotal, memswapused);
133 	}
134 
135 	splitmsg_done();
136 }
137 
138