summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2023-10-07 09:49:27 (GMT)
committerRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2023-10-07 09:49:27 (GMT)
commitb81847cb5f520ec23a1c907be330c1ad8eeeba2d (patch)
tree50df95c8174e5af8c0e884671dbcab4399cc20bd
parent19dc0039365e5cae0ed866c10202c50338f70b0a (diff)
downloadmonitoring-plugins-b81847cb5f520ec23a1c907be330c1ad8eeeba2d.tar.gz
Refactor new threshold parser
-rw-r--r--plugins-root/check_icmp.c82
1 files changed, 45 insertions, 37 deletions
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c
index 7140aa5..7c04ef2 100644
--- a/plugins-root/check_icmp.c
+++ b/plugins-root/check_icmp.c
@@ -216,6 +216,7 @@ static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, s
216static int send_icmp_ping(int, struct rta_host *); 216static int send_icmp_ping(int, struct rta_host *);
217static int get_threshold(char *str, threshold *th); 217static int get_threshold(char *str, threshold *th);
218static bool get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode); 218static bool get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode);
219static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, threshold_mode mode);
219static void run_checks(void); 220static void run_checks(void);
220static void set_source_ip(char *); 221static void set_source_ip(char *);
221static int add_target(char *); 222static int add_target(char *);
@@ -1880,59 +1881,66 @@ get_threshold(char *str, threshold *th)
1880static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) { 1881static bool get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) {
1881 if (!str || !length || !warn || !crit) return false; 1882 if (!str || !length || !warn || !crit) return false;
1882 1883
1883 char *p = NULL;
1884 bool first_iteration = true;
1885 1884
1886 // p points to the last char in str 1885 // p points to the last char in str
1887 p = &str[length - 1]; 1886 char *p = &str[length - 1];
1888 1887
1889 // first_iteration is bof-stop on stupid libc's 1888 // first_iteration is bof-stop on stupid libc's
1889 bool first_iteration = true;
1890
1890 while(p != &str[0]) { 1891 while(p != &str[0]) {
1891 if( (*p == 'm') || (*p == '%') ) { 1892 if( (*p == 'm') || (*p == '%') ) {
1892 *p = '\0'; 1893 *p = '\0';
1893 } else if(*p == ',' && !first_iteration) { 1894 } else if(*p == ',' && !first_iteration) {
1894 *p = '\0'; /* reset it so get_timevar(str) works nicely later */ 1895 *p = '\0'; /* reset it so get_timevar(str) works nicely later */
1895 1896
1896 switch (mode) { 1897 char *start_of_value = p + 1;
1897 case const_rta_mode: 1898
1898 crit->rta = atof(p+1)*1000; 1899 if (!parse_threshold2_helper(start_of_value, strlen(start_of_value), crit, mode)){
1899 break; 1900 return false;
1900 case const_packet_loss_mode:
1901 crit->pl = (unsigned char)strtoul(p+1, NULL, 0);
1902 break;
1903 case const_jitter_mode:
1904 crit->jitter = atof(p+1);
1905 break;
1906 case const_mos_mode:
1907 crit->mos = atof(p+1);
1908 break;
1909 case const_score_mode:
1910 crit->score = atof(p+1);
1911 break;
1912 } 1901 }
1902
1913 } 1903 }
1914 first_iteration = false; 1904 first_iteration = false;
1915 p--; 1905 p--;
1916 } 1906 }
1917 1907
1918 switch (mode) { 1908 return parse_threshold2_helper(p, strlen(p), warn, mode);
1919 case const_rta_mode: 1909}
1920 warn->rta = atof(p)*1000; 1910
1921 break; 1911static bool parse_threshold2_helper(char *s, size_t length, threshold *thr, threshold_mode mode) {
1922 case const_packet_loss_mode: 1912 char *resultChecker = {0};
1923 warn->pl = (unsigned char)strtoul(p, NULL, 0); 1913
1924 break; 1914 switch (mode) {
1925 case const_jitter_mode: 1915 case const_rta_mode:
1926 warn->jitter = atof(p); 1916 thr->rta = strtod(s, &resultChecker) * 1000;
1927 break; 1917 break;
1928 case const_mos_mode: 1918 case const_packet_loss_mode:
1929 warn->mos = atof(p); 1919 thr->pl = (unsigned char)strtoul(s, &resultChecker, 0);
1930 break; 1920 break;
1931 case const_score_mode: 1921 case const_jitter_mode:
1932 warn->score = atof(p); 1922 thr->jitter = strtod(s, &resultChecker);
1933 break; 1923
1934 } 1924 break;
1935 return true; 1925 case const_mos_mode:
1926 thr->mos = strtod(s, &resultChecker);
1927 break;
1928 case const_score_mode:
1929 thr->score = strtod(s, &resultChecker);
1930 break;
1931 }
1932
1933 if (resultChecker == s) {
1934 // Failed to parse
1935 return false;
1936 }
1937
1938 if (resultChecker != (s + length)) {
1939 // Trailing symbols
1940 return false;
1941 }
1942
1943 return true;
1936} 1944}
1937 1945
1938unsigned short 1946unsigned short