|
| 1 | +#include <stdio.h> |
| 2 | +#include <time.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <linux/rtc.h> |
| 6 | + |
| 7 | + |
| 8 | +//check hardware clock and sys clock |
| 9 | +//check if ntpdate -d will be a better fix command. then hwclock or date -s |
| 10 | +//issue 1 what happens if the system time is completely wrong? |
| 11 | +//check ntp status on old servers and chronyd on new. |
| 12 | +int main() { |
| 13 | + |
| 14 | +int hour[50], fixthis=0, htime , stime, n=0 , i; |
| 15 | + |
| 16 | +char clean1[100] , precheck1[100]; |
| 17 | + |
| 18 | + |
| 19 | +//clean existing temp file |
| 20 | +FILE *prep = popen("rm -rf /var/tmp/check_clock.txt", "r"); |
| 21 | + |
| 22 | +// check hardware clock and add stdout to array |
| 23 | +// change verbose to debug for older systems |
| 24 | +FILE *ls = popen("/usr/sbin/hwclock --debug | grep read", "r"); |
| 25 | + char buf[256]; |
| 26 | + while (fgets(buf, sizeof(buf), ls) != 0) { |
| 27 | + //printf("System Time : %s", buf); |
| 28 | + for (i=0; i < 52; i++) { |
| 29 | + // printf("%c", buf[i]); |
| 30 | + |
| 31 | + hour[n] = buf[i]; |
| 32 | + n = n + 1; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +pclose(ls); |
| 38 | +// validate is ntp or chronyd is running . |
| 39 | +// need to fix this to check ntpd instead |
| 40 | +FILE *pre1 = popen("/etc/init.d/ntpd status ", "r"); |
| 41 | + |
| 42 | + while (fgets(precheck1, sizeof(precheck1), ls) != 0) { |
| 43 | + // printf("NTP is: %c%c%c%c%c%c%c\n", precheck1[20],precheck1[21],precheck1[22],precheck1[23],precheck1[24],precheck1[25],precheck1[26]); |
| 44 | + char precheck2[50]; |
| 45 | + int live = snprintf(precheck2, sizeof(precheck2), "NTP is: %c%c%c%c%c%c%c", precheck1[20],precheck1[21],precheck1[22],precheck1[23],precheck1[24],precheck1[25],precheck1[26]); |
| 46 | +// compare the output string |
| 47 | + char out2[50]; |
| 48 | + char out3[50]; |
| 49 | + int rel; |
| 50 | + strcpy(out2, "NTP is: running"); |
| 51 | + strcpy(out3, "running"); |
| 52 | + rel = strcmp(out2, precheck2); |
| 53 | + if (rel == 0) { |
| 54 | + printf("NTP is running. %d\n", rel); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +pclose(pre1); |
| 60 | + |
| 61 | +FILE *pre2 = popen("/etc/init.d/ntpd status ", "r"); |
| 62 | + |
| 63 | + while (fgets(precheck1, sizeof(precheck1), ls) != 0) { |
| 64 | + // printf("NTP is: %c%c%c%c%c%c%c\n", precheck1[8],precheck1[9],precheck1[10],precheck1[11],precheck1[12], \ |
| 65 | + precheck1[13],precheck1[14]); |
| 66 | + char precheck2[50]; |
| 67 | + int live = snprintf(precheck2, sizeof(precheck2), "NTP is in a %c%c%c%c%c%c%c state", precheck1[8],precheck1[9],precheck1[10],precheck1[11],precheck1[12], \ |
| 68 | + precheck1[13],precheck1[14]); |
| 69 | +// compare the output string |
| 70 | + char out2[50]; |
| 71 | + char out3[50]; |
| 72 | + int rel; |
| 73 | + strcpy(out2, "NTP is in a stopped state"); |
| 74 | + strcpy(out3, "running"); |
| 75 | + rel = strcmp(out2, precheck2); |
| 76 | + if (rel == 0) { |
| 77 | + printf("NTP is not running.Terminating application %d\n", rel); |
| 78 | + fixthis = fixthis + 1; |
| 79 | + } |
| 80 | +} |
| 81 | +pclose(pre2); |
| 82 | +if (fixthis == 1) { |
| 83 | +FILE *auto_15 = popen("/usr/sbin/ntpd ", "r"); |
| 84 | +pclose(auto_15); |
| 85 | +exit(0); |
| 86 | +} |
| 87 | +//for testing only. |
| 88 | +//printf("Testing Sysclock with Char %c%c%c%c\n" , hour[42],hour[43],hour[45],hour[46]); |
| 89 | + |
| 90 | +//converting char to int |
| 91 | +int r = hour[42] - '0'; |
| 92 | +int x = hour[43] - '0'; |
| 93 | +int y = hour[45] - '0'; |
| 94 | +int z = hour[46] - '0'; |
| 95 | + |
| 96 | +//putting the int together . printing htime is for debugging only |
| 97 | +htime= r * 1000 + x * 100 + y * 10 + z; |
| 98 | +int htimeH = r * 10 + x; |
| 99 | +int htimeM = y * 10 + z; |
| 100 | +//printf("%i\n", htimeM); |
| 101 | + |
| 102 | +//start comparision to os clock |
| 103 | + |
| 104 | +FILE *os_time = popen("date -u +%H%M", "r"); |
| 105 | +char buf1[256]; |
| 106 | +while (fgets(buf1, sizeof(buf1), os_time) != 0) { |
| 107 | + //print char fro array- not needed unless debugging |
| 108 | + //printf("OS Time : %s", buf1); |
| 109 | + |
| 110 | +} |
| 111 | +pclose(os_time); |
| 112 | + |
| 113 | + |
| 114 | +int a = buf1[0] - '0'; |
| 115 | +int b = buf1[1] - '0'; |
| 116 | +int c = buf1[2] - '0'; |
| 117 | +int d = buf1[3] - '0'; |
| 118 | + |
| 119 | +stime= a * 1000 + b * 100 + c * 10 + d; |
| 120 | +int stimeH = a * 10 + b; |
| 121 | +int stimeM = c * 10 + d; |
| 122 | +//printf("%i",stimeM); |
| 123 | +if (htime == stime) { |
| 124 | + printf("%s", "The SYS clock and HWclock are in sync\n"); |
| 125 | + |
| 126 | +} |
| 127 | +else if (stime != htime) { |
| 128 | + printf("%s" , "The OS and HW clocks are not in sync.The sync is within the threshold\n"); |
| 129 | + //consider removing this check |
| 130 | +// if (htimeH != stimeH) { |
| 131 | +// printf("%s", "The hour is off and needed to be fixed. \n"); |
| 132 | +// FILE *temp = popen("touch /var/tmp/fixclock.txt" , "r"); |
| 133 | +// } |
| 134 | + // if the hardware clock is higher. how do we verify if the clock is correct |
| 135 | + if (htimeM > stimeM) { |
| 136 | + int timeD = htimeM - stimeM; |
| 137 | + //need time offset if the system time is not utc |
| 138 | + int timeF = htimeH - 4; |
| 139 | + if (timeD > 10) { |
| 140 | + printf("%s%i%s", "The hwCLOCK ahead of the sysclock by ", timeD , " minutes.Self correcting\n"); |
| 141 | + int fix1 = snprintf(clean1, sizeof(clean1), "hwclock --systohc"); |
| 142 | + FILE *FIX2 = popen(clean1, "r"); |
| 143 | + FILE *temp = popen("touch /var/tmp/check_clock.txt" , "r"); |
| 144 | + FILE *temp2 = popen("echo $(date) The clock was off by more then 10 minutes >> /var/tmp/clock.log" , "r"); |
| 145 | + |
| 146 | + } |
| 147 | + } |
| 148 | + // possible patch for minutes in the 50 range (if htimeM < 50 && htimeM < stimeM) |
| 149 | + // original line (htimeM < stimeM) |
| 150 | + else if (htimeM < stimeM) { |
| 151 | + int timeD = stimeM - htimeM; |
| 152 | + int timeF = htimeH - 4; |
| 153 | + if (timeD > 10) { |
| 154 | + printf("%s%i%s", "The HWCLOCK behind the sysclock by ", timeD , " minutes.Self Correcting\n"); |
| 155 | + // int fix1 = snprintf(clean1, sizeof(clean1), "date -s \"%d:%d\"",htimeH,htimeM); |
| 156 | + int fix1 = snprintf(clean1, sizeof(clean1), "hwclock --systohc"); |
| 157 | + FILE *FIX2 = popen(clean1, "r"); |
| 158 | + FILE *temp = popen("touch /var/tmp/check_clock.txt" , "r"); |
| 159 | + FILE *temp2 = popen("echo $(date) The clock was off by more then 10 minutes >> /var/tmp/clock.log" , "r"); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | +} |
0 commit comments