diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-10-06 15:08:52 +0200 |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-10-06 15:08:52 +0200 |
commit | 6d7d9a87aa74f00ed5603e13ebf96bb2b72e084a (patch) | |
tree | ae5633359b2293fe6d05e4c4b56ca533b717305b /plugins-root/check_icmp.c | |
parent | faf8e6a7915342bfa621900f0cf6f44eed8f88d0 (diff) | |
download | monitoring-plugins-6d7d9a87aa74f00ed5603e13ebf96bb2b72e084a.tar.gz |
Refactor get_threshold2 to be barely understandable
Diffstat (limited to 'plugins-root/check_icmp.c')
-rw-r--r-- | plugins-root/check_icmp.c | 100 |
1 files changed, 65 insertions, 35 deletions
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c index 197ce32f..79b93571 100644 --- a/plugins-root/check_icmp.c +++ b/plugins-root/check_icmp.c | |||
@@ -176,6 +176,16 @@ typedef union icmp_packet { | |||
176 | #define MODE_ALL 2 | 176 | #define MODE_ALL 2 |
177 | #define MODE_ICMP 3 | 177 | #define MODE_ICMP 3 |
178 | 178 | ||
179 | enum enum_threshold_mode { | ||
180 | const_rta_mode, | ||
181 | const_packet_loss_mode, | ||
182 | const_jitter_mode, | ||
183 | const_mos_mode, | ||
184 | const_score_mode | ||
185 | }; | ||
186 | |||
187 | typedef enum enum_threshold_mode threshold_mode; | ||
188 | |||
179 | /* the different ping types we can do | 189 | /* the different ping types we can do |
180 | * TODO: investigate ARP ping as well */ | 190 | * TODO: investigate ARP ping as well */ |
181 | #define HAVE_ICMP 1 | 191 | #define HAVE_ICMP 1 |
@@ -205,7 +215,7 @@ static int wait_for_reply(int, u_int); | |||
205 | static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval*); | 215 | static int recvfrom_wto(int, void *, unsigned int, struct sockaddr *, u_int *, struct timeval*); |
206 | static int send_icmp_ping(int, struct rta_host *); | 216 | static int send_icmp_ping(int, struct rta_host *); |
207 | static int get_threshold(char *str, threshold *th); | 217 | static int get_threshold(char *str, threshold *th); |
208 | static int get_threshold2(char *str, threshold *, threshold *, int type); | 218 | static int get_threshold2(char *str, size_t length, threshold *, threshold *, threshold_mode mode); |
209 | static void run_checks(void); | 219 | static void run_checks(void); |
210 | static void set_source_ip(char *); | 220 | static void set_source_ip(char *); |
211 | static int add_target(char *); | 221 | static int add_target(char *); |
@@ -581,23 +591,23 @@ main(int argc, char **argv) | |||
581 | exit (STATE_UNKNOWN); | 591 | exit (STATE_UNKNOWN); |
582 | break; | 592 | break; |
583 | case 'R': /* RTA mode */ | 593 | case 'R': /* RTA mode */ |
584 | get_threshold2(optarg, &warn, &crit,1); | 594 | get_threshold2(optarg, strlen(optarg), &warn, &crit, const_rta_mode); |
585 | rta_mode=true; | 595 | rta_mode=true; |
586 | break; | 596 | break; |
587 | case 'P': /* packet loss mode */ | 597 | case 'P': /* packet loss mode */ |
588 | get_threshold2(optarg, &warn, &crit,2); | 598 | get_threshold2(optarg, strlen(optarg), &warn, &crit, const_packet_loss_mode); |
589 | pl_mode=true; | 599 | pl_mode=true; |
590 | break; | 600 | break; |
591 | case 'J': /* jitter mode */ | 601 | case 'J': /* jitter mode */ |
592 | get_threshold2(optarg, &warn, &crit,3); | 602 | get_threshold2(optarg, strlen(optarg), &warn, &crit, const_jitter_mode); |
593 | jitter_mode=true; | 603 | jitter_mode=true; |
594 | break; | 604 | break; |
595 | case 'M': /* MOS mode */ | 605 | case 'M': /* MOS mode */ |
596 | get_threshold2(optarg, &warn, &crit,4); | 606 | get_threshold2(optarg, strlen(optarg), &warn, &crit, const_mos_mode); |
597 | mos_mode=true; | 607 | mos_mode=true; |
598 | break; | 608 | break; |
599 | case 'S': /* score mode */ | 609 | case 'S': /* score mode */ |
600 | get_threshold2(optarg, &warn, &crit,5); | 610 | get_threshold2(optarg, strlen(optarg), &warn, &crit, const_score_mode); |
601 | score_mode=true; | 611 | score_mode=true; |
602 | break; | 612 | break; |
603 | case 'O': /* out of order mode */ | 613 | case 'O': /* out of order mode */ |
@@ -1834,43 +1844,63 @@ get_threshold(char *str, threshold *th) | |||
1834 | 1844 | ||
1835 | /* not too good at checking errors, but it'll do (main() should barfe on -1) */ | 1845 | /* not too good at checking errors, but it'll do (main() should barfe on -1) */ |
1836 | static int | 1846 | static int |
1837 | get_threshold2(char *str, threshold *warn, threshold *crit, int type) | 1847 | get_threshold2(char *str, size_t length, threshold *warn, threshold *crit, threshold_mode mode) |
1838 | { | 1848 | { |
1849 | if (!str || !length || !warn || !crit) return -1; | ||
1850 | |||
1839 | char *p = NULL; | 1851 | char *p = NULL; |
1840 | bool i = false; | 1852 | bool first_iteration = true; |
1853 | |||
1854 | // pointer magic slims code by 10 lines. i is bof-stop on stupid libc's | ||
1855 | // p points to the last char in str | ||
1856 | p = &str[length - 1]; | ||
1841 | 1857 | ||
1842 | if(!str || !strlen(str) || !warn || !crit) return -1; | ||
1843 | /* pointer magic slims code by 10 lines. i is bof-stop on stupid libc's */ | ||
1844 | p = &str[strlen(str) - 1]; | ||
1845 | while(p != &str[0]) { | 1858 | while(p != &str[0]) { |
1846 | if( (*p == 'm') || (*p == '%') ) *p = '\0'; | 1859 | if( (*p == 'm') || (*p == '%') ) { |
1847 | else if(*p == ',' && i) { | 1860 | *p = '\0'; |
1861 | } else if(*p == ',' && !first_iteration) { | ||
1848 | *p = '\0'; /* reset it so get_timevar(str) works nicely later */ | 1862 | *p = '\0'; /* reset it so get_timevar(str) works nicely later */ |
1849 | if (type==1) | 1863 | |
1850 | crit->rta = atof(p+1)*1000; | 1864 | switch (mode) { |
1851 | else if (type==2) | 1865 | case const_rta_mode: |
1852 | crit->pl = (unsigned char)strtoul(p+1, NULL, 0); | 1866 | crit->rta = atof(p+1)*1000; |
1853 | else if (type==3) | 1867 | break; |
1854 | crit->jitter = atof(p+1); | 1868 | case const_packet_loss_mode: |
1855 | else if (type==4) | 1869 | crit->pl = (unsigned char)strtoul(p+1, NULL, 0); |
1856 | crit->mos = atof(p+1); | 1870 | break; |
1857 | else if (type==5) | 1871 | case const_jitter_mode: |
1858 | crit->score = atof(p+1); | 1872 | crit->jitter = atof(p+1); |
1873 | break; | ||
1874 | case const_mos_mode: | ||
1875 | crit->mos = atof(p+1); | ||
1876 | break; | ||
1877 | case const_score_mode: | ||
1878 | crit->score = atof(p+1); | ||
1879 | break; | ||
1880 | } | ||
1859 | } | 1881 | } |
1860 | i = true; | 1882 | first_iteration = false; |
1861 | p--; | 1883 | p--; |
1862 | } | 1884 | } |
1863 | if (type==1) | 1885 | |
1864 | warn->rta = atof(p)*1000; | 1886 | switch (mode) { |
1865 | else if (type==2) | 1887 | case const_rta_mode: |
1866 | warn->pl = (unsigned char)strtoul(p, NULL, 0); | 1888 | warn->rta = atof(p)*1000; |
1867 | if (type==3) | 1889 | break; |
1868 | warn->jitter = atof(p); | 1890 | case const_packet_loss_mode: |
1869 | else if (type==4) | 1891 | warn->pl = (unsigned char)strtoul(p, NULL, 0); |
1870 | warn->mos = atof(p); | 1892 | break; |
1871 | else if (type==5) | 1893 | case const_jitter_mode: |
1872 | warn->score = atof(p); | 1894 | warn->jitter = atof(p); |
1873 | return 0; | 1895 | break; |
1896 | case const_mos_mode: | ||
1897 | warn->mos = atof(p); | ||
1898 | break; | ||
1899 | case const_score_mode: | ||
1900 | warn->score = atof(p); | ||
1901 | break; | ||
1902 | } | ||
1903 | return 0; | ||
1874 | } | 1904 | } |
1875 | 1905 | ||
1876 | unsigned short | 1906 | unsigned short |