vi check_domain_expire.c
#include <stdio.h>
#include <time.h>
#include <string.h>
#define LEN 100L
#define GREP_CMD "whois xx-xxstore.com | grep 'Registrar Registration Expiration Date'"
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
int strtotime(char datetime[LEN]) {
char *str;
int timestamp;
struct tm tm_time;
// str=strptime("2015-07-01 18:00:00", "%Y-%m-%d %H:%M:%S", &tm_time);
str=strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time);
if(str==NULL) {
fprintf(stderr,"strptime() error.\n");
return -1;
}
timestamp=mktime(&tm_time);
if(timestamp==-1) {
fprintf(stderr,"mktime() error.\n");
return -1;
}
return timestamp;
}
int main() {
int ret;
FILE *fp;
char *str;
char readbuf[LEN];
char tmp1[LEN];
char tmp2[LEN];
char expire_time[LEN];
int expire_days;
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char status_information[LEN];
char performance_data[LEN];
time_t now_time;
now_time=time(NULL);
// printf("now_time=%ld\n",now_time);
fp=popen(GREP_CMD,"r");
if(fp==NULL) {
fprintf(stderr,"popen() error.\n");
return -1;
}
str=fgets(readbuf,LEN,fp);
if(str==NULL) {
fprintf(stderr,"fgets() error.\n");
}
// printf("readbuf=%s",readbuf);
// ret=sscanf(readbuf,"Registrar Registration Expiration Date: %sT%sZ",tmp1,tmp2);
// ret=sscanf(readbuf,"Registrar Registration Expiration Date: %s",tmp1);
ret=sscanf(readbuf,"Registrar Registration Expiration Date: %[0-9-]",tmp1);
if(ret==0) {
fprintf(stderr,"sscanf() error.\n");
return -1;
}
// printf("tmp1=%s,tmp2=%s\n",tmp1,tmp2);
// printf("tmp1=%s\n",tmp1);
ret=sscanf(readbuf,"Registrar Registration Expiration Date: %*[^T]T%[^Z]",tmp2);
// printf("tmp2=%s\n",tmp2);
memset(expire_time,0,LEN);
sprintf(expire_time,"%s %s",tmp1,tmp2);
// printf("expire_time=%s\n",expire_time);
ret=pclose(fp);
if(ret!=0) {
fprintf(stderr,"pclose() error.\n");
return -1;
}
ret=strtotime(expire_time);
if(ret==-1) {
fprintf(stderr,"strtotime() error.\n");
}
// printf("expire_timestamp=%d\n",ret);
// printf("expire seconds=%ld\n",ret-now_time);
// printf("expire days=%ld\n",(ret-now_time)/86400);
expire_days=(ret-now_time)/86400;
if(expire_days>60) {
exitstatus=OK;
}
else if(expire_days<=60 && expire_days>30) {
exitstatus=WARNING;
}
else if(expire_days<=400 && expire_days>=0) {
exitstatus=CRITICAL;
}
else {
exitstatus=UNKNOWN;
}
sprintf(status_information,"xx-xxstore.com domain will expire on %s", expire_time);
// sprintf(performance_data,"cur_all_session=%s;;;; tcp_all_session=%s;;;; tcp_half_open=%s;;;; tcp_half_close=%s;;;; udp_session=%s;;;; icmp_session=%s;;;; rawip_session=%s;;;;", cur_session, tcp_session, half_open, half_close, udp_session, icmp_session, rawip_session);
// printf("%s - %s | %s\n", exit_status[exitstatus], status_information, performance_data);
printf("%s - %s\n", exit_status[exitstatus], status_information);
return exitstatus;
}