summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent/sysinfo.c
blob: 04b332af8f119bdf7feb5c983d949a0a73aa45a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#include "config.h"

#ifdef HAVE_STDIO_H
	#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
	#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
	#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
	#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
	#include <sys/types.h>
#endif
#ifdef HAVE_DIRENT_H
	#include <dirent.h>
#endif
/* Linux */
#ifdef HAVE_SYS_VFS_H
	#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_SYSINFO_H
	#include <sys/sysinfo.h>
#endif
/* Solaris */
#ifdef HAVE_SYS_STATVFS_H
	#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_LOADAVG_H
	#include <sys/loadavg.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
	#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
	#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
	#include <arpa/inet.h>
#endif
/* OpenBSD */
#ifdef HAVE_SYS_PARAM_H
	#include <sys/param.h>
#endif

#ifdef HAVE_SYS_MOUNT_H
	#include <sys/mount.h>
#endif

/* HP-UX */
#ifdef HAVE_SYS_PSTAT_H
	#include <sys/pstat.h>
#endif

#ifdef HAVE_NETDB_H
	#include <netdb.h>
#endif

#include <string.h>

#include "common.h"
#include "sysinfo.h"


COMMAND	commands[]=
	{
	{"proc_cnt[apache]"		,PROCCNT, "apache"},
	{"proc_cnt[inetd]"		,PROCCNT, "inetd"},
	{"proc_cnt[mysqld]"		,PROCCNT, "mysqld"},
	{"proc_cnt[syslogd]"		,PROCCNT, "syslogd"},
	{"proc_cnt[sshd]"		,PROCCNT, "sshd"},

	{"proc_cnt[zabbix_agentd]"	,PROCCNT, "zabbix_agentd"},
	{"proc_cnt[zabbix_suckerd]"	,PROCCNT, "zabbix_suckerd"},
	{"proc_cnt[zabbix_trapperd]"	,PROCCNT, "zabbix_trapperd"},

	{"memory[total]"		,TOTALMEM, 0},
	{"memory[shared]"		,SHAREDMEM, 0},
	{"memory[buffers]"		,BUFFERSMEM, 0},
	{"memory[cached]"		,CACHEDMEM, 0},
	{"memory[free]"			,FREEMEM, 0},

	{"diskfree[/]"			,DF, "/"},
	{"diskfree[/opt]"		,DF, "/opt"},
	{"diskfree[/tmp]"		,DF, "/tmp"},
	{"diskfree[/usr]"		,DF, "/usr"},
	{"diskfree[/home]"		,DF, "/home"},
	{"diskfree[/var]"		,DF, "/var"},

	{"inodefree[/]"			,INODE, "/"},
	{"inodefree[/opt]"		,INODE, "/opt"},
	{"inodefree[/tmp]"		,INODE, "/tmp"},
	{"inodefree[/usr]"		,INODE, "/usr"},
	{"inodefree[/home]"		,INODE, "/home"},
	{"inodefree[/var]"		,INODE, "/var"},

	{"cksum[/etc/inetd_conf]"	,EXECUTE, "(cksum /etc/inetd.conf 2>/dev/null || echo '-2') | cut -f1 -d' '"},
	{"cksum[/etc/services]"		,EXECUTE, "(cksum /etc/services 2>/dev/null || echo '-2') | cut -f1 -d' '"},
	{"cksum[/vmlinuz]"		,EXECUTE, "(cksum /vmlinuz 2>/dev/null || echo '-2') | cut -f1 -d' '"},
	{"cksum[/etc/passwd]"		,EXECUTE, "(cksum /etc/passwd 2>/dev/null || echo '-2') | cut -f1 -d' '"},
	{"cksum[/usr/sbin/sshd]"	,EXECUTE, "(cksum /usr/sbin/sshd 2>/dev/null || echo '-2') | cut -f1 -d' '"},
	{"cksum[/usr/bin/ssh]"		,EXECUTE, "(cksum /usr/bin/ssh 2>/dev/null || echo '-2') | cut -f1 -d' '"},

	{"filesize[/var/log/syslog]"	,FILESIZE, "/var/log/syslog"},

	{"swap[free]"			,SWAPFREE, 0},
	{"swap[total]"			,SWAPTOTAL, 0},

/****************************************
  	All these perameters require more than 1 second to retrieve.

  	{"swap[in]"			,EXECUTE, "vmstat -n 1 2|tail -1|cut -b37-40"},
	{"swap[out]"			,EXECUTE, "vmstat -n 1 2|tail -1|cut -b41-44"},

	{"system[interrupts]"		,EXECUTE, "vmstat -n 1 2|tail -1|cut -b57-61"},
	{"system[switches]"		,EXECUTE, "vmstat -n 1 2|tail -1|cut -b62-67"},
***************************************/

	{"io[disk_io]"			,DISK_IO,  0},
	{"io[disk_rio]"			,DISK_RIO, 0},
	{"io[disk_wio]"			,DISK_WIO, 0},
	{"io[disk_rblk]"		,DISK_RBLK, 0},
	{"io[disk_wblk]"		,DISK_WBLK, 0},

	{"system[procload]"		,PROCLOAD, 0},
	{"system[procload5]"		,PROCLOAD5, 0},
	{"system[procload15]"		,PROCLOAD15, 0},
	{"system[proccount]"		,PROCCOUNT, 0},
	{"system[procrunning]"		,EXECUTE, "cat /proc/loadavg|cut -f1 -d'/'|cut -f4 -d' '"},
	{"system[uptime]"		,UPTIME, 0},
	{"system[users]"		,EXECUTE, "who|wc -l"},

	{"ping"				,PING, 0},
	{"tcp_count"			,EXECUTE, "netstat -tn|grep EST|wc -l"},

	{"net[listen_23]"		,TCP_LISTEN, "0017"},
	{"net[listen_80]"		,TCP_LISTEN, "0050"},

	{"check_service[ssh]"		,CHECK_SERVICE_SSH, 0},
	{"check_service[smtp]"		,CHECK_SERVICE_SMTP, 0},
	{"check_service[ftp]"		,CHECK_SERVICE_FTP, 0},
	{"check_service[pop]"		,CHECK_SERVICE_POP, 0},
	{"check_service[nntp]"		,CHECK_SERVICE_NNTP, 0},
	{"check_service[imap]"		,CHECK_SERVICE_IMAP, 0},
	{0				,0}
	};

void	test_parameters(void)
{
	int	i;
	float	result;
	float	(*function)();
	char	*parameter = NULL;
	char	*key = NULL;

	i=0;
	while(0 != commands[i].function)
	{
		key=commands[i].key;
		function=commands[i].function;
		parameter=commands[i].parameter;

		result = function(parameter);
		if( result == FAIL )
		{
			printf("\tUNSUPPORTED Key: %s\n",key);
		}
		else
		{
			printf("SUPPORTED Key: %s [%f]\n",key,result);
		}

		i++;
	}
}

float	process(char *command)
{
	char	*p;
	float	result;
	int	i;
	float	(*function)();
	char	*parameter = NULL;

	for( p=command+strlen(command)-1; p>command && ( *p=='\r' || *p =='\n' || *p == ' ' ); --p );
	p[1]=0;
	
	i=0;
	for(;;)
	{
		if( commands[i].key == 0)
		{
			function=0;
			break;
		}
		if( strcmp(commands[i].key,command) == 0)
		{
			function=commands[i].function;
			parameter=commands[i].parameter;
			break;
		}	
		i++;
	}

	if( function !=0 )
	{
		result = function(parameter);
		if( result == FAIL )
		{
			result = NOTSUPPORTED;
		}
	}
	else
	{
		result=NOTSUPPORTED;
	}

	return	result;
}

float   FILESIZE(const char * filename)
{
	struct stat	buf;

	if(stat(filename,&buf) == 0)
	{
		return	buf.st_size;
	}

	return	FAIL;
}

float	PROCCNT(const char * procname)
{
#ifdef	HAVE_PROC_1_STATUS
	DIR	*dir;
	struct	dirent *entries;
	struct	stat buf;
	char	filename[512];
	char	line[512];
	char	*name;

	FILE	*f;

	int	proccount=0;

	dir=opendir("/proc");
	while((entries=readdir(dir))!=NULL)
	{
		strcpy(filename,"/proc/");	
		strcat(filename,entries->d_name);
		strcat(filename,"/status");

		if(stat(filename,&buf)==0)
		{
			fflush(stdout);
			f=fopen(filename,"r");
			if(f==NULL)
			{
				continue;
			}
			fgets(line,512,f);
			fclose(f);

/*			sscanf(line,"%s\t%s\n",line,name);
			printf("%s\n",name);
			fflush(stdout);*/

			name=(char *)strtok(line,"\t");
			name=(char *)strtok(NULL,"\t");

			name[strlen(name)-1]=0;

			if(strcmp(procname,name)==0)
			{
				proccount++;
			}
		}
	}
	closedir(dir);
	return	(float)proccount;
#else
	return	FAIL;
#endif
}

float	INODE(const char * mountPoint)
{
#ifdef HAVE_SYS_STATVFS_H
	struct statvfs   s;

	if ( statvfs( (char *)mountPoint, &s) != 0 )
	{
		return  FAIL;
	}

	return  s.f_favail;
#else
	struct statfs   s;
	long            blocks_used;
	long            blocks_percent_used;

	if ( statfs( (char *)mountPoint, &s) != 0 ) 
	{
		return	FAIL;
	}
        
	if ( s.f_blocks > 0 ) {
		blocks_used = s.f_blocks - s.f_bfree;
		blocks_percent_used = (long)
		(blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);

//		printf(
//		"%7.0f %7.0f  %7.0f  %5ld%%   %s\n"
//		,s.f_blocks * (s.f_bsize / 1024.0)
//		,(s.f_blocks - s.f_bfree)  * (s.f_bsize / 1024.0)
//		,s.f_bavail * (s.f_bsize / 1024.0)
//		,blocks_percent_used
//		,mountPoint);
		return s.f_ffree;

	}

	return	FAIL;
#endif
}

float	DF(const char * mountPoint)
{
#ifdef HAVE_SYS_STATVFS_H
	struct statvfs   s;

	if ( statvfs( (char *)mountPoint, &s) != 0 )
	{
		return  FAIL;
	}

	return  s.f_bsize*s.f_bavail;
#else
	struct statfs   s;
	long            blocks_used;
	long            blocks_percent_used;

	if ( statfs( (char *)mountPoint, &s) != 0 )
	{
		return	FAIL;
	}
        
	if ( s.f_blocks > 0 ) {
		blocks_used = s.f_blocks - s.f_bfree;
		blocks_percent_used = (long)
		(blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);

//		printf(
//		"%7.0f %7.0f  %7.0f  %5ld%%   %s\n"
//		,s.f_blocks * (s.f_bsize / 1024.0)
//		,(s.f_blocks - s.f_bfree)  * (s.f_bsize / 1024.0)
//		,s.f_bavail * (s.f_bsize / 1024.0)
//		,blocks_percent_used
//		,mountPoint);
		return s.f_bavail * (s.f_bsize / 1024.0);

	}

	return	FAIL;
#endif
}

float	TCP_LISTEN(const char *porthex)
{
#ifdef HAVE_PROC
	FILE	*f;
	char	c[1024];

	char	pattern[1024]="0050 00000000:0000 0A";

	strcpy(pattern,porthex);
	strcat(pattern," 00000000:0000 0A");

	f=fopen("/proc/net/tcp","r");
	if(NULL == f)
	{
		return	FAIL;
	}

	while (NULL!=fgets(c,1024,f))
	{
		if(NULL != strstr(c,pattern))
		{
			fclose(f);
			return 1;
		}
	}
	fclose(f);

	return	0;
#else
	return	FAIL;
#endif
}

#ifdef	HAVE_PROC
float	getPROC(char *file,int lineno,int fieldno)
{
	FILE	*f;
	char	*t;
	char	c[1024];
	float	result;
	int	i;

	f=fopen(file,"r");
	if(NULL == f)
	{
		return	FAIL;
	}
	for(i=1;i<=lineno;i++)
	{	
		fgets(c,1024,f);
	}
	t=(char *)strtok(c," ");
	for(i=2;i<=fieldno;i++)
	{
		t=(char *)strtok(NULL," ");
	}
	fclose(f);

	sscanf(t, "%f", &result );

	return	result;
}
#endif

float	CACHEDMEM(void)
{
#ifdef HAVE_PROC
	return getPROC("/proc/meminfo",8,2);
#else
	return FAIL;
#endif
}

float	BUFFERSMEM(void)
{
#ifdef HAVE_SYSINFO_BUFFERRAM
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.bufferram;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	SHAREDMEM(void)
{
#ifdef HAVE_SYSINFO_SHAREDRAM
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.sharedram;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	TOTALMEM(void)
{
#ifdef HAVE_SYS_PSTAT_H
	struct	pst_static pst;
	long	page;

	if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1)
	{
		return FAIL;
	}
	else
	{
		/* Get page size */	
		page = pst.page_size;
		/* Total physical memory in bytes */	
		return page*pst.physical_memory;
	}
#else
#ifdef HAVE_SYSINFO_TOTALRAM
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.totalram;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
#endif
}

float	FREEMEM(void)
{
#ifdef HAVE_SYS_PSTAT_H
	struct	pst_static pst;
	struct	pst_dynamic dyn;
	long	page;

	if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1)
	{
		return FAIL;
	}
	else
	{
		/* Get page size */	
		page = pst.page_size;
//		return pst.physical_memory;

		if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
		{
			return FAIL;
		}
		else
		{
//cout<<"total virtual memory allocated is " << dyn.psd_vm << "
//pages, " << dyn.psd_vm * page << " bytes" << endl;
//cout<<"active virtual memory is " << dyn.psd_avm <<" pages, " <<
//dyn.psd_avm * page << " bytes" << endl;
//cout<<"total real memory is " << dyn.psd_rm << " pages, " <<
//dyn.psd_rm * page << " bytes" << endl;
//cout<<"active real memory is " << dyn.psd_arm << " pages, " <<
//dyn.psd_arm * page << " bytes" << endl;
//cout<<"free memory is " << dyn.psd_free << " pages, " <<
		/* Free memory in bytes */	
			return dyn.psd_free * page;
		}
	}
#else
#ifdef HAVE_SYSINFO_FREERAM
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.freeram;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
#endif
}

float	UPTIME(void)
{
#ifdef HAVE_SYSINFO_UPTIME
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.uptime;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	PING(void)
{
	return	1;
}

float	PROCLOAD(void)
{
#ifdef HAVE_GETLOADAVG
	double	load[3];

	if(getloadavg(load, 3))
	{
		return load[0];	
	}
	else
	{
		return FAIL;	
	}
#else
#ifdef	HAVE_SYS_PSTAT_H
	struct	pst_dynamic dyn;

	if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
	{
		return FAIL;
	}
	else
	{
		return dyn.psd_avg_1_min;
	}
#else
#ifdef HAVE_PROC
	return	getPROC("/proc/loadavg",1,1);
#else
	return	FAIL;
#endif
#endif
#endif
}

float	PROCLOAD5(void)
{
#ifdef HAVE_GETLOADAVG
	double	load[3];

	if(getloadavg(load, 3))
	{
		return load[1];	
	}
	else
	{
		return FAIL;	
	}
#else
#ifdef	HAVE_SYS_PSTAT_H
	struct	pst_dynamic dyn;

	if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
	{
		return FAIL;
	}
	else
	{
		return dyn.psd_avg_5_min;
	}
#else
#ifdef	HAVE_PROC
	return	getPROC("/proc/loadavg",1,2);
#else
	return	FAIL;
#endif
#endif
#endif
}

float	PROCLOAD15(void)
{
#ifdef HAVE_GETLOADAVG
	double	load[3];

	if(getloadavg(load, 3))
	{
		return load[2];	
	}
	else
	{
		return FAIL;	
	}
#else
#ifdef	HAVE_SYS_PSTAT_H
	struct	pst_dynamic dyn;

	if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1)
	{
		return FAIL;
	}
	else
	{
		return dyn.psd_avg_5_min;
	}
#else
#ifdef	HAVE_PROC
	return	getPROC("/proc/loadavg",1,3);
#else
	return	FAIL;
#endif
#endif
#endif
}

float	SWAPFREE(void)
{
#ifdef HAVE_SYSINFO_FREESWAP
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.freeswap;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	PROCCOUNT(void)
{
#ifdef HAVE_SYSINFO_PROCS
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	info.procs;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	SWAPTOTAL(void)
{
#ifdef HAVE_SYSINFO_TOTALSWAP
	struct sysinfo info;

	if( 0 == sysinfo(&info))
	{
		return	(float)info.totalswap;
	}
	else
	{
		return FAIL;
	}
#else
	return	FAIL;
#endif
}

float	DISK_IO(void)
{
#ifdef	HAVE_PROC
	return	getPROC("/proc/stat",2,2);
#else
	return	FAIL;
#endif
}

float	DISK_RIO(void)
{
#ifdef	HAVE_PROC
	return	getPROC("/proc/stat",3,2);
#else
	return	FAIL;
#endif
}

float	DISK_WIO(void)
{
#ifdef	HAVE_PROC
	return	getPROC("/proc/stat",4,2);
#else
	return	FAIL;
#endif
}

float	DISK_RBLK(void)
{
#ifdef	HAVE_PROC
	return	getPROC("/proc/stat",5,2);
#else
	return	FAIL;
#endif
}

float	DISK_WBLK(void)
{
#ifdef	HAVE_PROC
	return	getPROC("/proc/stat",6,2);
#else
	return	FAIL;
#endif
}

float	EXECUTE(char *command)
{
	FILE	*f;
	float	result;
	char	c[1024];

	f=popen( command,"r");
	if(f==0)
	{
		return	FAIL;
	}
	fgets(c,1024,f);

	if(pclose(f) != 0)
	{
		return	FAIL;	
	}

	sscanf(c, "%f", &result );

	return	result;
}

float	tcp_expect(char	*hostname, short port, char *expect,char *sendtoclose)
{
	char	*haddr;
	char	c[1024];
	
	int	s;
	struct	sockaddr_in addr;
	int	addrlen;


	struct hostent *host;

	host = gethostbyname(hostname);
	if(host == NULL)
	{
		return	0;
	}

	haddr=host->h_addr;


	addrlen = sizeof(addr);
	memset(&addr, 0, addrlen);
	addr.sin_port = htons(port);
	addr.sin_family = AF_INET;
	bcopy(haddr, (void *) &addr.sin_addr.s_addr, 4);

	s = socket(AF_INET, SOCK_STREAM, 0);
	if (s == -1)
	{
		close(s);
		return	0;
	}

	if (connect(s, (struct sockaddr *) &addr, addrlen) == -1)
	{
		close(s);
		return	0;
	}

	memset(&c, 0, 1024);
	recv(s, c, 1024, 0);
	if (strncmp(c, expect, strlen(expect)) == 0)
	{
		send(s,sendtoclose,strlen(sendtoclose),0);
		close(s);
		return	1;
	}
	else
	{
		send(s,sendtoclose,strlen(sendtoclose),0);
		close(s);
		return	0;
	}
}

float	CHECK_SERVICE_SSH(void)
{
	return	tcp_expect("127.0.0.1",22,"SSH","0\n");
}

float	CHECK_SERVICE_SMTP(void)
{
	return	tcp_expect("127.0.0.1",25,"220","");
}

float	CHECK_SERVICE_FTP(void)
{
	return	tcp_expect("127.0.0.1",21,"220","");
}

float	CHECK_SERVICE_POP(void)
{
	return	tcp_expect("127.0.0.1",110,"+OK","");
}

float	CHECK_SERVICE_NNTP(void)
{
	return	tcp_expect("127.0.0.1",119,"220","");
}

float	CHECK_SERVICE_IMAP(void)
{
	return	tcp_expect("127.0.0.1",143,"* OK","a1 LOGOUT\n");
}