├── LICENSE ├── README.md ├── check_haproxy └── check_haproxy.icinga2.conf /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # check_haproxy Nagios Check Script 2 | 3 | This is a Nagios plugin to check the status of HAProxy over a socket connection 4 | (by default, /var/run/haproxy.sock) and parse the statistics to ensure that it 5 | is operating within accepted limits, as per the defaults, or as set for each 6 | frontend/backend. 7 | 8 | ## Usage 9 | 10 | check_haproxy [--defaults (defaults)] [--overrides (1 2 ... n)] 11 | [--[no]frontends] [--[no]backends] [--[no]servers] 12 | [--socket (path)] [--help] 13 | 14 | ### -f, --[no]frontends 15 | 16 | Enable/disable checks for the frontends in HAProxy (that they're marked as OPEN 17 | and the session limits haven't been reached). 18 | 19 | ### -b, --[no]backends 20 | 21 | Enable/disable checks for the backends in HAProxy (that they have the required 22 | quorum of servers, and that the session limits haven't been reached). 23 | 24 | ### -s, --[no]servers 25 | 26 | Enable/disable checks for the servers in HAProxy (that they haven't reached the 27 | limits for the sessions or for queues). 28 | 29 | ### -D, --defaults (defaults) 30 | 31 | Set/Override the defaults which will be applied to all checks (unless 32 | specifically set by --overrides). Takes the form: 33 | 34 | {u,d,x},{svr_warn},{svr_crit},{sess_warn},{sess_crit} 35 | 36 | Each of these is optional, but the positioning is important. To fully override, 37 | set (for example): 38 | 39 | u,10,5,.25,0.5 40 | 41 | which means: 42 | 43 | * `u`: Check for servers up (`u`) or servers down (`d`), or disable all checks 44 | on that particular frontend/backend (`x`). 45 | * `10`: WARNING if less than 10 servers are up, or if at least that many servers 46 | are down. 47 | * `5`: CRITICAL if less than 5 servers are available, or if at least that many 48 | have gone away. 49 | * `.25`: WARNING if more any frontend, backend, or individual server has gone 50 | over 25% of it's maximum allowed sessions (or any queue for any server on the 51 | backend is at least 25% full). 52 | * `0.5`: CRITICAL for the same reasons as previous, but we've reached 50% of 53 | these levels. 54 | 55 | To override only some of these values from the pre-set defaults 56 | (`u,5,2,.75,.9`), simply leave the others as empty, for example: `,10,7` will 57 | leave checks as up, but increase the server WARN/CRIT to 10/7. or to switch to 58 | use down, use `d,`, or off with `x`. 59 | 60 | Each number has two meanings: 61 | 62 | * `less than 1`: Where any number is less than (but not equal to) 1, then the 63 | value is understood as a percentage of the available servers or session 64 | limits. If you have 20 servers and set a threshold of .25 then that would 65 | mean 5 servers either up or down. 66 | * `greater than, or equal to 1`: Where any number is greater than, or equal 67 | to, 1, then this is a fixed value. By setting 5 you are hard-coding the 68 | number of servers or sessions, regardless of the limit or number available 69 | to HAProxy. 70 | 71 | The code **does not** alter this limit if it is greater than the available 72 | servers or sessions so there are situations where the alert may not trigger. 73 | For example, in the event of `down` being the trigger and the number of servers 74 | is less than the Warning or Critical thresholds, the error may never trigger. 75 | It is generally better to use `up` and percentage values where you are going 76 | to have a flexible number of backends. 77 | 78 | ### -O, --overrides (override override ... override) 79 | 80 | Override the defaults for a particular frontend or backend, in the form 81 | `name:override`, where `override` is the same format as `--defaults` above. For 82 | example, to override the frontend called "api" and allow that to increase to 83 | limits of 15/10 for WARN/CRIT, use `api:,15,10`. Add as many as you like as 84 | space-delimited options: 85 | 86 | --overrides api:,15,10 assets:d,2,5 webmail:u,3,2 87 | 88 | ### -S, --socket /path/to/socket 89 | 90 | Path to the socket `check_haproxy` should connect to (requires read/write 91 | permissions and must be at least user level; no operator or admin privileges 92 | needed). 93 | 94 | ## Icinga2 configuration 95 | 96 | Copy `check_haproxy.icinga2.conf` to the icinga2 zone and define a new service for all Linux hosts with `vars.haproxy`, for example: 97 | 98 | ``` 99 | apply Service "Haproxy stats" { 100 | import "generic-service" 101 | check_command = "haproxy" 102 | vars.haproxy_socket = "/var/run/haproxy/admin.sock" 103 | vars.haproxy_default = "C" 104 | command_endpoint = host.vars.client_endpoint 105 | assign where host.vars.client_endpoint && host.vars.os == "Linux" && host.vars.haproxy 106 | } 107 | ``` 108 | 109 | ## Licence 110 | 111 | This program is free software; you can redistribute it and/or 112 | modify it under the terms of the GNU General Public License 113 | as published by the Free Software Foundation; either version 2 114 | of the License, or (at your option) any later version. 115 | 116 | This program is distributed in the hope that it will be useful, 117 | but WITHOUT ANY WARRANTY; without even the implied warranty of 118 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 119 | GNU General Public License for more details. 120 | 121 | You should have received a copy of the GNU General Public License 122 | along with this program; if not, write to the Free Software 123 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 124 | -------------------------------------------------------------------------------- /check_haproxy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | # check_haproxy Nagios Plugin 4 | # Nagios Plugin to check the state of HAProxy. 5 | # (c) 2014-2015 Jonathan Wright 6 | # 7 | # This program is free software; you can redistribute it and/or 8 | # modify it under the terms of the GNU General Public License 9 | # as published by the Free Software Foundation; either version 2 10 | # of the License, or (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program; if not, write to the Free Software 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | 21 | # Configure the Perl Environment 22 | use warnings; 23 | use Carp; 24 | use Switch; 25 | 26 | # Import Modules 27 | use Getopt::Long qw(:config no_ignore_case); 28 | use Pod::Usage; 29 | use IO::Socket::UNIX qw( SOCK_STREAM ); 30 | use Data::Dumper; 31 | use Scalar::Util qw(looks_like_number); 32 | 33 | # Configure Program details 34 | our ($_program, $_version, $_author); 35 | $_program = 'check_haproxy'; 36 | $_version = '1.0.3'; 37 | $_author = 'Jonathan Wright '; 38 | 39 | use constant { 40 | # Field names to locations for all the CSV data provided by HAProxy 41 | STAT_PROXY_NAME => 0, 42 | STAT_SERVICE_NAME => 1, 43 | STAT_QUEUED_REQUESTS => 2, 44 | STAT_QUEUED_MAX => 3, 45 | STAT_SESSIONS_CURRENT => 4, 46 | STAT_SESSIONS_MAX => 5, 47 | STAT_SESSIONS_LIMIT => 6, 48 | STAT_CONNECTIONS_TOTAL => 7, 49 | STAT_BYTES_IN => 8, 50 | STAT_BYTES_OUT => 9, 51 | STAT_REQUESTS_DENIED => 10, 52 | STAT_RESPONSES_DENIED => 11, 53 | STAT_REQUESTS_ERROR => 12, 54 | STAT_CONNECTIONS_ERROR => 13, 55 | STAT_RESPONSES_ERROR => 14, 56 | STAT_CONNECTIONS_RETRIED => 15, 57 | STAT_CONNECTIONS_REDISPATCHED => 16, 58 | STAT_STATUS => 17, 59 | STAT_WEIGHT => 18, 60 | STAT_SERVER_ACTIVE => 19, STAT_SERVERS_ACTIVE => 19, 61 | STAT_SERVER_BACKUP => 20, STAT_SERVERS_BACKUP => 20, 62 | STAT_CHECKS_FAIL => 21, 63 | STAT_CHECKS_GO_DOWN => 22, 64 | STAT_CHECKS_LAST_CHANGE => 23, 65 | STAT_CHECKS_DOWNTIME => 24, 66 | STAT_QUEUED_LIMIT => 25, 67 | STAT_PID => 26, 68 | STAT_UID => 27, 69 | STAT_SID => 28, 70 | STAT_THROTTLE => 29, 71 | STAT_SESSIONS_TOTAL => 30, 72 | STAT_TRACKED => 31, 73 | STAT_SERVICE_TYPE => 32, 74 | STAT_SESSIONS_RATE_CURRENT => 33, 75 | STAT_SESSIONS_RATE_LIMIT => 34, 76 | STAT_SESSIONS_RATE_MAX => 35, 77 | STAT_CHECK_STATUS => 36, 78 | STAT_CHECK_CODE => 37, 79 | STAT_CHECK_DURATION => 38, 80 | STAT_RESPONSES_HTTP_1XX => 39, 81 | STAT_RESPONSES_HTTP_2XX => 40, 82 | STAT_RESPONSES_HTTP_3XX => 41, 83 | STAT_RESPONSES_HTTP_4XX => 42, 84 | STAT_RESPONSES_HTTP_5XX => 43, 85 | STAT_RESPONSES_HTTP_XXX => 44, 86 | STAT_CHECK_FAILED_DETAILS => 45, 87 | STAT_REQUESTS_RATE_CURRENT => 46, 88 | STAT_REQUESTS_RATE_MAX => 47, 89 | STAT_REQUESTS_TOTAL => 48, 90 | STAT_ABORTS_CLIENT => 49, 91 | STAT_ABORTS_SERVER => 50, 92 | STAT_COMPRESSOR_IN => 51, 93 | STAT_COMPRESSOR_OUT => 52, 94 | STAT_COMPRESSOR_BYPASSED => 53, 95 | STAT_COMPRESSOR_REQUESTS => 54, 96 | STAT_SESSIONS_LAST => 55, 97 | STAT_CHECK_HEALTH_LAST => 56, 98 | STAT_CHECK_AGENT_LAST => 57, 99 | STAT_TIME_QUEUE => 58, 100 | STAT_TIME_CONNECT => 59, 101 | STAT_TIME_RESPONSE => 60, 102 | STAT_TIME_TOTAL => 61, 103 | # Types used by HAProxy for some fields 104 | TYPE_FRONTEND => 0, 105 | TYPE_BACKEND => 1, 106 | TYPE_SERVER => 2, 107 | TYPE_LISTENER => 3, 108 | }; 109 | 110 | use constant { 111 | LIMIT_BACKEND_CHECK => 'u', 112 | LIMIT_WARN_BACKEND_UP => 5, 113 | LIMIT_CRIT_BACKEND_UP => 2, 114 | LIMIT_WARN_BACKEND_DOWN => 2, 115 | LIMIT_CRIT_BACKEND_DOWN => 5, 116 | LIMIT_WARN_LIMITS => 0.75, # Percentages 117 | LIMIT_CRIT_LIMITS => 0.90, 118 | }; 119 | 120 | use constant EXIT_STATUS => qw(OK WARNING CRITICAL UNKNOWN); 121 | 122 | use constant { 123 | EXIT_OK => 0, 124 | EXIT_WARNING => 1, 125 | EXIT_CRITICAL => 2, 126 | EXIT_UNKNOWN => 3, 127 | }; 128 | 129 | # Alias _ to sprintf 130 | sub _ {return sprintf(shift, @_)} 131 | 132 | # Create hash array for holding all configuration variables 133 | our (%opt, %data, %checks); 134 | %opt = ( 135 | 'check_frontends' => 1, 136 | 'check_backends' => 1, 137 | 'check_servers' => 1, 138 | 'socket' => '/var/run/haproxy.sock', 139 | 'debug' => 0, 140 | 'defaults' => undef, 141 | 'help' => 0, 142 | ); 143 | 144 | # Somewhere to store messages 145 | our (@critical, @warning); 146 | 147 | sub main { 148 | my (@overrides); 149 | 150 | GetOptions( 151 | 'f' => \$opt{'check_frontends'}, 'frontends!' => \$opt{'check_frontends'}, 152 | 'b' => \$opt{'check_backends'}, 'backends!' => \$opt{'check_backends'}, 153 | 's' => \$opt{'check_servers'}, 'servers!' => \$opt{'check_servers'}, 154 | 'S=s' => \$opt{'socket'}, 'socket=s' => \$opt{'socket'}, 155 | 'd' => \$opt{'debug'}, 'debug!' => \$opt{'debug'}, 156 | 'O=s{1,}' => \@overrides, 'overrides=s{1,}' => \@overrides, 157 | 'D=s' => \$opt{'defaults'}, 'defaults=s' => \$opt{'defaults'}, 158 | '-h' => \$opt{'help'}, '--help' => \$opt{'help'}, 159 | ) or pod2usage( 160 | -message => sprintf("%s %s by %s", $_program, $_version, $_author), 161 | -exitval => EXIT_UNKNOWN, 162 | -verbose => 0); 163 | 164 | pod2usage( 165 | -message => sprintf("%s %s by %s", $_program, $_version, $_author), 166 | -exitval => EXIT_UNKNOWN, 167 | -verbose => 1) if $opt{'help'}; 168 | 169 | my $socket = IO::Socket::UNIX->new( 170 | Peer => $opt{'socket'}, 171 | Type => SOCK_STREAM, 172 | Timeout => 1, 173 | ) or do_unknown("Cannot connect to socket ".$opt{'socket'}, 1); 174 | 175 | %data = get_data($socket); 176 | %checks = build_checks(\@overrides); 177 | 178 | check_frontends() if $opt{'check_frontends'}; 179 | check_backends() if $opt{'check_backends'}; 180 | check_servers() if $opt{'check_servers'}; 181 | 182 | my ($frontends, $backends, $servers, $services) = (0,0,0,0); 183 | my (%servers); 184 | 185 | foreach my $service (sort keys %data) { 186 | switch ($data{$service}{'type'}) { 187 | case TYPE_FRONTEND { 188 | ++$frontends; 189 | } 190 | case TYPE_BACKEND { 191 | ++$backends; 192 | foreach my $server (sort keys %{$data{$service}{'servers'}}) { 193 | next if $data{$service}{'servers'}{$server}{'backup'}; 194 | ++$services; 195 | unless (exists $servers{$server}) { 196 | ++$servers; 197 | $servers{$server} = 1; 198 | } 199 | } 200 | } 201 | } 202 | } 203 | 204 | do_success( 205 | _('HAProxy is functioning within established parameters. '. 206 | '(%d frontends, %d backends, %d servers, %d services)', 207 | $frontends, $backends, $servers, $services) 208 | ); 209 | } 210 | 211 | sub _debug { 212 | return unless $opt{'debug'}; 213 | printf "%s{} %s\n", shift, shift; 214 | } 215 | 216 | sub _report { 217 | my ($status, $message) = (shift, shift); 218 | croak('No message given') unless $message; 219 | _debug('report', "with status ${status} and message '${message}'"); 220 | 221 | printf "HAPROXY %s: %s (%s %s)\n", (EXIT_STATUS)[$status], $message, $_program, $_version; 222 | exit $status; 223 | } 224 | 225 | sub do_success { 226 | _report(EXIT_OK, shift); 227 | } 228 | 229 | sub do_warning { 230 | my ($message) = shift; 231 | push(@warning, $message); 232 | _report(EXIT_WARNING, $message); 233 | } 234 | 235 | sub do_critical { 236 | my ($message, $_croak) = (shift, shift); 237 | croak($message) if $opt{'debug'} and $_croak; 238 | push(@critical, $message); 239 | _report(EXIT_CRITICAL, $message); 240 | } 241 | 242 | sub do_unknown { 243 | my ($message) = shift; 244 | confess($message) if $opt{'debug'}; 245 | _report(EXIT_UNKNOWN, $message); 246 | } 247 | 248 | sub check_pass { 249 | my ($state, $threshold, $value, $max) = (shift, shift, shift, shift); 250 | 251 | # Bypass any checks 252 | return 0 if $state eq 'x'; 253 | 254 | # Sanity checking 255 | do_unknown( 256 | _('Negative check value for state %s: %d', $state, $value) 257 | ) if $value < 0; 258 | 259 | do_unknown( 260 | _('Negative check threshold for state %s: %d', $state, $threshold) 261 | ) if $threshold < 0; 262 | 263 | do_unknown( 264 | _('Negative check maximum for state %s: %d', $state, $max) 265 | ) if $max < 0; 266 | 267 | do_unknown( 268 | _('No max value provided for percentage threshold on state %d: %s', 269 | $state, $max || 'undef') 270 | ) if ($threshold < 1 and $threshold > 0) 271 | and (not $max or not $max =~ /^[0-9]+(\.[0-9]+)?$/); 272 | 273 | switch ($state) { 274 | case 'u' { 275 | if ($threshold < 1 and $threshold > 0) { 276 | _debug('check_pass', _("UP: %d >= (%.1f%% of %d)", $value, ($threshold*100), $max)); 277 | return $value >= ($max * $threshold); 278 | } else { 279 | _debug('check_pass', _("UP: %d >= %d", $value, $threshold)); 280 | return $value >= $threshold; 281 | } 282 | } 283 | case 'd' { 284 | if ($threshold < 1 and $threshold > 0) { 285 | _debug('check_pass', _("DOWN: %d >= (%.1f%% of %d)", $value, ($threshold*100), $max)); 286 | return $value < ($max * $threshold); 287 | } else { 288 | _debug('check_pass', _("DOWN: %d < %d", $value, $threshold)); 289 | return $value < $threshold; 290 | } 291 | } 292 | case 't' { 293 | _debug('check_pass', _("Threshold: %d < (%.1f%% of %d)", $value, ($threshold*100), $max)); 294 | return $value < ($max * $threshold); 295 | } 296 | default { 297 | # More sanity checking 298 | do_unknown("Invalid state check: ${state}"); 299 | } 300 | } 301 | } 302 | 303 | sub get_data { 304 | my ($s, %v); $s = shift; 305 | 306 | _debug('get_data', 'is reading data from HAProxy'); 307 | 308 | print $s "show stat\n"; 309 | while (<$s>) { 310 | chomp; next unless length; 311 | my @stat = split (','); 312 | 313 | switch ($stat[STAT_SERVICE_TYPE]) { 314 | # Process all FRONTEND type entries; these are singular and are totals 315 | # only for each of the frontends configured 316 | case TYPE_FRONTEND { 317 | $v{$stat[STAT_PROXY_NAME]} = { 318 | type => TYPE_FRONTEND, 319 | connections => $stat[STAT_CONNECTIONS_TOTAL], 320 | sessions => { 321 | current => $stat[STAT_SESSIONS_CURRENT], 322 | limit => ($stat[STAT_SESSIONS_LIMIT] ? $stat[STAT_SESSIONS_LIMIT] : 0), 323 | }, 324 | status => $stat[STAT_STATUS], 325 | queued => 0, 326 | }; 327 | } 328 | # Process all BACKEND type entries; these are the totals for each backend 329 | # and don't have the same amount of information as SERVERs 330 | case TYPE_BACKEND { 331 | # We can't 'set' the hash here as the backend totals are normally after 332 | # the backend's servers, so would override anything previously set 333 | # in TYPE_SERVER 334 | $v{$stat[STAT_PROXY_NAME]}{'type'} = TYPE_BACKEND; 335 | $v{$stat[STAT_PROXY_NAME]}{'connections'} = $stat[STAT_CONNECTIONS_TOTAL]; 336 | $v{$stat[STAT_PROXY_NAME]}{'sessions'} = { 337 | current => $stat[STAT_SESSIONS_CURRENT], 338 | limit => ($stat[STAT_SESSIONS_LIMIT] ? $stat[STAT_SESSIONS_LIMIT] : 0), 339 | }; 340 | $v{$stat[STAT_PROXY_NAME]}{'queued'} = $stat[STAT_QUEUED_REQUESTS]; 341 | $v{$stat[STAT_PROXY_NAME]}{'active'} = $stat[STAT_SERVERS_ACTIVE]; 342 | $v{$stat[STAT_PROXY_NAME]}{'backup'} = $stat[STAT_SERVERS_BACKUP]; 343 | } 344 | # Process all SERVER type entries, which are the most details and give 345 | # information about how each server is responding 346 | case TYPE_SERVER { 347 | # Only set the server itself directly, otherwise we may override 348 | # anything previously set 349 | $v{$stat[STAT_PROXY_NAME]}{'servers'}{$stat[STAT_SERVICE_NAME]} = { 350 | active => ($stat[STAT_SERVER_ACTIVE] ? 1 : 0), 351 | backup => ($stat[STAT_SERVER_BACKUP] ? 1 : 0), 352 | up => ($stat[STAT_STATUS] eq 'UP' ? 1 : 0), 353 | down => ($stat[STAT_STATUS] eq 'DOWN' ? 1 : 0), 354 | disabled => ($stat[STAT_STATUS] =~ /^(MAINT|DRAIN|NOLB)/i ? 1 : 0), 355 | connections => $stat[STAT_CONNECTIONS_TOTAL], 356 | sessions => { 357 | current => $stat[STAT_SESSIONS_CURRENT], 358 | limit => ($stat[STAT_SESSIONS_LIMIT] ? $stat[STAT_SESSIONS_LIMIT] : 0), 359 | }, 360 | queued => { 361 | current => $stat[STAT_QUEUED_REQUESTS], 362 | limit => ($stat[STAT_QUEUED_LIMIT] ? $stat[STAT_QUEUED_LIMIT] : 0), 363 | }, 364 | status => $stat[STAT_STATUS], 365 | timing => { 366 | queue => $stat[STAT_TIME_QUEUE], 367 | connect => $stat[STAT_TIME_CONNECT], 368 | response => $stat[STAT_TIME_RESPONSE], 369 | total => $stat[STAT_TIME_TOTAL] 370 | }, 371 | }; 372 | } 373 | } 374 | } 375 | 376 | return %v; 377 | } 378 | 379 | sub build_checks { 380 | my $overrides = shift; 381 | my @overrides = @{$overrides}; 382 | 383 | my (%checks, $state, $be_warn, $be_crit, $limit_warn, $limit_crit); 384 | 385 | # Setup the defaults for all checks 386 | switch (LIMIT_BACKEND_CHECK) { 387 | case 'u' { 388 | $state = 'u'; 389 | $be_warn = LIMIT_WARN_BACKEND_UP; 390 | $be_crit = LIMIT_CRIT_BACKEND_UP; 391 | } 392 | default { 393 | $state = 'd'; 394 | $be_warn = LIMIT_WARN_BACKEND_DOWN; 395 | $be_crit = LIMIT_CRIT_BACKEND_DOWN; 396 | } 397 | } 398 | 399 | $limit_warn = LIMIT_WARN_LIMITS; 400 | $limit_crit = LIMIT_CRIT_LIMITS; 401 | 402 | # See if there is an override for the defaults, then use those 403 | if (defined $opt{'defaults'} 404 | and $opt{'defaults'} =~ /([ud]?),(?:((?:0?\.)?[0-9]*)(?:,((?:0?\.)?[0-9]*)(?:,((?:0?\.)?[0-9]*)%?(?:,((?:0?\.)?[0-9]*)%?)?)?)?)?/) { 405 | $state = $1 if $1; 406 | $be_warn = $2 if looks_like_number($2); 407 | $be_crit = $3 if looks_like_number($3); 408 | $limit_warn = ($4/100) if looks_like_number($4); 409 | $limit_crit = ($5/100) if looks_like_number($5); 410 | _debug('build_checks', 411 | _('setting defaults to %s,%.2f,%.2f,%.2f,%.2f', 412 | $state, $be_warn, $be_crit, $limit_warn, $limit_crit)); 413 | } 414 | 415 | # Build the defaults for all checks 416 | foreach my $service (sort keys %data) { 417 | my $type; 418 | 419 | switch ($data{$service}{'type'}) { 420 | case TYPE_FRONTEND { 421 | $type = 'FRONTEND'; 422 | %{$checks{$service}} = ( 423 | limit_warn => $limit_warn, 424 | limit_crit => $limit_crit, 425 | ); 426 | } 427 | case TYPE_BACKEND { 428 | $type = 'BACKEND'; 429 | %{$checks{$service}} = ( 430 | state => $state, 431 | be_warn => $be_warn, 432 | be_crit => $be_crit, 433 | limit_warn => $limit_warn, 434 | limit_crit => $limit_crit, 435 | ); 436 | } 437 | } 438 | 439 | _debug('build_checks', _('building %s check for %s', $type, $service)); 440 | } 441 | 442 | # Merge the two arrays 443 | foreach my $override (sort @overrides) { 444 | my ($service, $state, $be_warn, $be_crit, $limit_warn, $limit_crit); 445 | 446 | _debug('build_checks', _('processing override %s', $override)); 447 | 448 | next unless ($override =~ /([^:]+):([udx]?)(?:,([0-9]*)(?:,([0-9]*)(?:,([0-9]*)%?(?:,([0-9]*)%?)?)?)?)?/); 449 | 450 | next unless exists $data{$1}; 451 | 452 | if ($data{$1}{'type'} eq TYPE_BACKEND) { 453 | $checks{$1}{'state'} = $2 if $2 and ($2 eq 'u' or $2 eq 'd' or $2 eq 'x'); 454 | $checks{$1}{'be_warn'} = $3 if looks_like_number($3) and $3 >= 0; 455 | $checks{$1}{'be_crit'} = $4 if looks_like_number($4) and $4 >= 0; 456 | } 457 | 458 | $checks{$1}{'limit_warn'} = ($5/100) if looks_like_number($5) and $5 > 0 and $5 <= 100; 459 | $checks{$1}{'limit_crit'} = ($6/100) if looks_like_number($6) and $6 > 0 and $6 <= 100; 460 | 461 | _debug('build_checks', 462 | _('setting override for %s to %s,%d,%d,%.2f,%.2f', 463 | $1, 464 | $checks{$1}{'state'}, 465 | $checks{$1}{'be_warn'}, 466 | $checks{$1}{'be_crit'}, 467 | $checks{$1}{'limit_warn'}, 468 | $checks{$1}{'limit_crit'})); 469 | } 470 | 471 | return %checks; 472 | } 473 | 474 | sub check_frontends { 475 | _debug('check_frontends', 'is starting check on frontends'); 476 | foreach my $service (sort keys %data) { 477 | next unless $data{$service}{'type'} eq TYPE_FRONTEND; 478 | _debug('check_frontends', _('checking FRONTEND %s', $service)); 479 | 480 | _debug('check_frontends', 'running OPEN check'); 481 | 482 | do_critical(_('FRONTEND %s is %s', $service, $data{$service}{'status'})) 483 | unless $data{$service}{'status'} eq 'OPEN'; 484 | 485 | # Theses no supported Queueing on the Frontends, so no checks for that 486 | # need to be done here, just for Sessions. 487 | 488 | next unless $data{$service}{'sessions'}{'limit'} > 0; 489 | _debug('check_frontends', 'running Sessions check'); 490 | _debug('check_frontends', _('found %d (limit %d); thresholds are %.1f%%, %.1f%%', 491 | $data{$service}{'sessions'}{'current'}, 492 | $data{$service}{'sessions'}{'limit'}, 493 | ($checks{$service}{'limit_warn'}*100), 494 | ($checks{$service}{'limit_crit'}*100))); 495 | 496 | do_critical(_('Sessions for FRONTEND %s has passed %.2f%% of Limit (%d active, %d limit)', 497 | $service, ($checks{$service}{'limit_crit'}*100), 498 | $data{$service}{'sessions'}{'current'}, 499 | $data{$service}{'sessions'}{'limit'})) 500 | unless check_pass('t', $checks{$service}{'limit_crit'}, 501 | $data{$service}{'sessions'}{'current'}, 502 | $data{$service}{'sessions'}{'limit'}); 503 | 504 | do_warning(_('Sessions for FRONTEND %s has passed %.2f%% of Limit (%d active, %d limit)', 505 | $service, ($checks{$service}{'limit_warn'}*100), 506 | $data{$service}{'sessions'}{'current'}, 507 | $data{$service}{'sessions'}{'limit'})) 508 | unless check_pass('t', $checks{$service}{'limit_warn'}, 509 | $data{$service}{'sessions'}{'current'}, 510 | $data{$service}{'sessions'}{'limit'}); 511 | 512 | } 513 | 514 | } 515 | 516 | sub check_backends { 517 | _debug('check_backends', 'is starting check on backends'); 518 | # Run checks for each of the backends 519 | foreach my $service (sort keys %data) { 520 | next unless $data{$service}{'type'} eq TYPE_BACKEND; 521 | next if $checks{$service}{'state'} eq 'x'; 522 | _debug('check_backends', _('checking BACKEND %s', $service)); 523 | 524 | my ($msg, $total, $up, $down, $disabled) = ('',0,0,0,0); 525 | 526 | foreach my $server (sort keys %{$data{$service}{'servers'}}) { 527 | ++$total unless $data{$service}{'servers'}{$server}{'backend'}; 528 | ++$up if $data{$service}{'servers'}{$server}{'up'}; 529 | ++$down if $data{$service}{'servers'}{$server}{'down'}; 530 | ++$disabled if $data{$service}{'servers'}{$server}{'disabled'}; 531 | } 532 | 533 | _debug('check_backends', _('found %d up, %d down, %d disabled (%d total)', 534 | $up, $down, $disabled, $total)); 535 | 536 | switch ($checks{$service}{'state'}) { 537 | case 'u' { 538 | $msg = 'BACKEND %s has fallen below %d available server(s) (%d up, %d down, %d disabled)'; 539 | } 540 | case 'd' { 541 | $msg = 'BACKEND %s has passed %d down/disabled servers (%d up, %d down, %d disabled)', 542 | } 543 | } 544 | 545 | do_critical(_($msg, $service, $checks{$service}{'be_crit'}, $up, $down, $disabled)) 546 | unless check_pass($checks{$service}{'state'}, $checks{$service}{'be_crit'}, 547 | ($checks{$service}{'state'} eq 'u' ? $up : ($down+$disabled)), $total); 548 | do_warning(_($msg, $service, $checks{$service}{'be_warn'}, $up, $down, $disabled)) 549 | unless check_pass($checks{$service}{'state'}, $checks{$service}{'be_warn'}, 550 | ($checks{$service}{'state'} eq 'u' ? $up : ($down+$disabled)), $total); 551 | 552 | next unless $data{$service}{'sessions'}{'limit'} > 0; 553 | _debug('check_frontends', 'running Sessions check'); 554 | _debug('check_backends', _('found %d (limit %d); threshold is %.1f%% (warn) or %.1f%% (crit)', 555 | $data{$service}{'sessions'}{'current'}, 556 | $data{$service}{'sessions'}{'limit'}, 557 | ($checks{$service}{'limit_warn'}*100), 558 | ($checks{$service}{'limit_crit'}*100))); 559 | 560 | do_critical(_('Sessions for BACKEND %s has passed %d%% of Limit (%d active, %d limit)', 561 | $service, ($checks{$service}{'limit_crit'}*100), 562 | $data{$service}{'sessions'}{'current'}, 563 | $data{$service}{'sessions'}{'limit'})) 564 | unless check_pass('t', $checks{$service}{'limit_crit'}, 565 | $data{$service}{'sessions'}{'current'}, 566 | $data{$service}{'sessions'}{'limit'}); 567 | 568 | do_warning(_('Sessions for BACKEND %s has passed %d%% of Limit (%d active, %d limit)', 569 | $service, ($checks{$service}{'limit_warn'}*100), 570 | $data{$service}{'sessions'}{'current'}, 571 | $data{$service}{'sessions'}{'limit'})) 572 | unless check_pass('t', $checks{$service}{'limit_warn'}, 573 | $data{$service}{'sessions'}{'current'}, 574 | $data{$service}{'sessions'}{'limit'}); 575 | 576 | } 577 | } 578 | 579 | sub check_servers { 580 | _debug('check_servers', 'is starting check on servers'); 581 | # Run checks for each of servers in each of the backends 582 | foreach my $service (sort keys %data) { 583 | next unless $data{$service}{'type'} eq TYPE_BACKEND; 584 | next if $checks{$service}{'state'} eq 'x'; 585 | foreach my $server (sort keys %{$data{$service}{'servers'}}) { 586 | next if $data{$service}{'servers'}{$server}{'backend'}; 587 | _debug('check_servers', _('checking SERVER %s on BACKEND %s', $server, $service)); 588 | 589 | if ($data{$service}{'servers'}{$server}{'sessions'}{'limit'} > 0) { 590 | _debug('check_servers', 'running Sessions check'); 591 | _debug('check_servers', _('found %d (limit %d); thresholds are %.1f%%, %.1f%%', 592 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 593 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'}, 594 | ($checks{$service}{'limit_warn'}*100), 595 | ($checks{$service}{'limit_crit'}*100))); 596 | 597 | do_critical(_('Sessions for SERVER %s on BACKEND %s has passed %d%% of Limit (%d active, %d limit)', 598 | $server, $service, ($checks{$service}{'limit_crit'}*100), 599 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 600 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'})) 601 | unless check_pass('t', $checks{$service}{'limit_crit'}, 602 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 603 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'}); 604 | 605 | do_warning(_('Sessions for SERVER %s on BACKEND %s has passed %d%% of Limit (%d active, %d limit)', 606 | $server, $service, ($checks{$service}{'limit_warn'}*100), 607 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 608 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'})) 609 | unless check_pass('t', $checks{$service}{'limit_warn'}, 610 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 611 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'}); 612 | 613 | } 614 | 615 | if ($data{$service}{'servers'}{$server}{'queued'}{'limit'} > 0) { 616 | _debug('check_servers', 'running Queue check'); 617 | _debug('check_servers', _('found %d (limit %d); thresholds are %.1f%%, %.1f%%', 618 | $data{$service}{'servers'}{$server}{'sessions'}{'current'}, 619 | $data{$service}{'servers'}{$server}{'sessions'}{'limit'}, 620 | ($checks{$service}{'limit_warn'}*100), 621 | ($checks{$service}{'limit_crit'}*100))); 622 | 623 | do_critical(_('Queue for SERVER %s on BACKEND has passed %d%% of Limit (%d active, %d limit)', 624 | $server, $service, ($checks{$service}{'limit_crit'}*100), 625 | $data{$service}{'servers'}{$server}{'queued'}{'current'}, 626 | $data{$service}{'servers'}{$server}{'queued'}{'limit'})) 627 | unless check_pass('t', $checks{$service}{'limit_crit'}, 628 | $data{$service}{'servers'}{$server}{'queued'}{'current'}, 629 | $data{$service}{'servers'}{$server}{'queued'}{'limit'}); 630 | 631 | do_warning(_('Queue for SERVER %s on BACKEND %s has passed %d%% of Limit (%d active, %d limit)', 632 | $server, $service, ($checks{$service}{'limit_warn'}*100), 633 | $data{$service}{'servers'}{$server}{'queued'}{'current'}, 634 | $data{$service}{'servers'}{$server}{'queued'}{'limit'})) 635 | unless check_pass('t', $checks{$service}{'limit_warn'}, 636 | $data{$service}{'servers'}{$server}{'queued'}{'current'}, 637 | $data{$service}{'servers'}{$server}{'queued'}{'limit'}); 638 | } 639 | } 640 | } 641 | } 642 | 643 | # Run the program 644 | &main(); 645 | 646 | __END__ 647 | 648 | =head1 NAME 649 | 650 | check_haproxy: This is a Nagios plugin to check the status of HAProxy over a 651 | socket connection (by default, /var/run/haproxy.sock). 652 | 653 | =head1 SYNOPSIS 654 | 655 | check_haproxy [--defaults (defaults)] [--overrides S<(1 2 ... n)>] 656 | [--[no]frontends] [--[no]backends] [--[no]servers] 657 | [--socket (path)] [--help] 658 | 659 | =head1 OPTIONS 660 | 661 | =over 662 | 663 | =item B<-f>, B<--[no]frontends> 664 | 665 | Enable/disable checks for the frontends in HAProxy (that they're marked as OPEN 666 | and the session limits haven't been reached). 667 | 668 | =item B<-b>, B<--[no]backends> 669 | 670 | Enable/disable checks for the backends in HAProxy (that they have the required 671 | quorum of servers, and that the session limits haven't been reached). 672 | 673 | =item B<-s>, B<--[no]servers> 674 | 675 | Enable/disable checks for the servers in HAProxy (that they haven't reached the 676 | limits for the sessions or for queues). 677 | 678 | =item B<-D>, B<--defaults> I 679 | 680 | Set/Override the defaults which will be applied to all checks (unless 681 | specifically set by --overrides). Takes the form: 682 | 683 | C<{u,d,x},{svr_warn},{svr_crit},{sess_warn},{sess_crit}> 684 | 685 | Each of these is optional, but the positioning is important. To fully override, 686 | set (for example): 687 | 688 | C 689 | 690 | which means: 691 | 692 | =over 693 | 694 | =item C 695 | 696 | Check for servers up (C) or servers down (C), or disable all checks on 697 | that particular frontend/backend (C). 698 | 699 | =item C<10> 700 | 701 | WARNING if less than 10 servers are up, or if at least that many servers are 702 | down. 703 | 704 | =item C<5> 705 | 706 | CRITICAL if less than 5 servers are available, or if at least that many have 707 | gone away. 708 | 709 | =item C<.25> 710 | 711 | WARNING if more any frontend, backend, or individual server has gone over 25% of 712 | it's maximum allowed sessions (or any queue for any server on the backend is at 713 | least 25% full). 714 | 715 | =item C<0.5> 716 | 717 | CRITICAL for the same reasons as previous, but we've reached 50% of these 718 | levels. 719 | 720 | =back 721 | 722 | To override only some of these values from the pre-set defaults 723 | (C), simply leave the others as empty, for example: C<,10,7> will 724 | leave checks as up, but increase the server WARN/CRIT to 10/7. or to switch to 725 | use down, use C, or off with C. 726 | 727 | Each number has two meanings: 728 | 729 | =over 730 | 731 | =item C 732 | 733 | Where any number is less than (but not equal to) 1, then the value is understood 734 | as a percentage of the available servers or session limits. If you have 20 735 | servers and set a threshold of .25 then that would mean 5 servers either up or 736 | down. 737 | 738 | =item C 739 | 740 | Where any number is greater than, or equal to, 1, then this is a fixed value. 741 | By setting 5 you are hard-coding the number of servers or sessions, regardless 742 | of the limit or number available to HAProxy. 743 | 744 | The code B alter this limit if it is greater than the available 745 | servers or sessions so there are situations where the alert may not trigger. 746 | For example, in the event of C being the trigger and the number of servers 747 | is less than the Warning or Critical thresholds, the error may never trigger. 748 | It is generally better to use C and percentage values where you are going 749 | to have a flexible number of backends. 750 | 751 | =back 752 | 753 | =item B<-O>, B<--overrides> I 754 | 755 | Override the defaults for a particular frontend or backend, in the form 756 | {name}:{override}, where {override} is the same format as --defaults above. For 757 | example, to override the frontend called "api" and allow that to increase to 758 | limits of 15/10 for WARN/CRIT, use C. Add as many as you like as 759 | space-delimited options: 760 | 761 | C<--overrides api:,15,10 assets:d,2,5 webmail:u,3,2> 762 | 763 | =item B<-S>, B<--socket> I 764 | 765 | Path to the socket check_haproxy should connect to (requires read/write 766 | permissions and must be at least user level; no operator or admin privileges 767 | needed). 768 | 769 | =back 770 | 771 | =head1 DESCRIPTION 772 | 773 | B will connect to the HAProxy socket and parse the statistics to 774 | ensure that it is operating within accepted limits, as per the defaults, or as 775 | set for each frontend/backend. 776 | 777 | =head1 HISTORY 778 | 779 | =over 780 | 781 | =item v1.0.0rc1 782 | 783 | =over 784 | 785 | =item * 786 | 787 | Testing Release. 788 | 789 | =back 790 | 791 | =item v1.0.0rc2 792 | 793 | =over 794 | 795 | =item * 796 | 797 | Fix bug in calling do_unknown(). 798 | 799 | =back 800 | 801 | =item v1.0.0 802 | 803 | =over 804 | 805 | =item * 806 | 807 | Added support for disabling some checks with an x state. 808 | 809 | =back 810 | 811 | =item v1.0.1 812 | 813 | =over 814 | 815 | =item * 816 | 817 | Added prefix to response message. 818 | 819 | =back 820 | 821 | =item v1.0.2 822 | 823 | =over 824 | 825 | =item * 826 | 827 | Removed old constants no longer needed. 828 | 829 | =item * 830 | 831 | Added help POD messages. 832 | 833 | =back 834 | 835 | =item v1.0.3 836 | 837 | =over 838 | 839 | =item * 840 | 841 | Improved error messages around check_pass. 842 | 843 | =item * 844 | 845 | Fixed critical messaeg for sessions levels: Missing substitution in the report 846 | string. 847 | 848 | =item * 849 | 850 | Remove sanity check forcing a maximum value to be blow any threshold as it seams 851 | that in some cases this is possible in HAProxy. 852 | 853 | =back 854 | 855 | =back 856 | 857 | =end text 858 | 859 | =head1 AUTHOR 860 | 861 | Jonathan Wright 862 | 863 | =head1 COPYRIGHT AND LICENCE 864 | 865 | (c) 2014-2015, Jonathan Wright 866 | 867 | This program is free software; you can redistribute it and/or 868 | modify it under the terms of the GNU General Public License 869 | as published by the Free Software Foundation; either version 2 870 | of the License, or (at your option) any later version. 871 | 872 | This program is distributed in the hope that it will be useful, 873 | but WITHOUT ANY WARRANTY; without even the implied warranty of 874 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 875 | GNU General Public License for more details. 876 | 877 | You should have received a copy of the GNU General Public License 878 | along with this program; if not, write to the Free Software 879 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 880 | 881 | =cut 882 | -------------------------------------------------------------------------------- /check_haproxy.icinga2.conf: -------------------------------------------------------------------------------- 1 | object CheckCommand "haproxy" { 2 | import "plugin-check-command" 3 | command = [ PluginDir + "/check_haproxy" ] 4 | 5 | arguments = { 6 | "--defaults" = { 7 | value = "$haproxy_default$" 8 | description = "Set/Override the defaults which will be applied to all checks (unless specifically set by --overrides)." 9 | } 10 | "--frontends" = { 11 | set_if = "$haproxy_frontends$" 12 | description = "Enable checks for the frontends in HAProxy (that they're marked as OPEN and the session limits haven't been reached)." 13 | } 14 | "--nofrontends" = { 15 | set_if = "$haproxy_nofrontends$" 16 | description = "Disable checks for the frontends in HAProxy (that they're marked as OPEN and the session limits haven't been reached)." 17 | } 18 | "--backends" = { 19 | set_if = "$haproxy_backends$" 20 | description = "Enable checks for the backends in HAProxy (that they have the required quorum of servers, and that the session limits haven't been reached)." 21 | } 22 | "--nobackends" = { 23 | set_if = "$haproxy_nobackends$" 24 | description = "Disable checks for the backends in HAProxy (that they have the required quorum of servers, and that the session limits haven't been reached)." 25 | } 26 | "--servers" = { 27 | set_if = "$haproxy_servers$" 28 | description = "Enable checks for the servers in HAProxy (that they haven't reached the limits for the sessions or for queues)." 29 | } 30 | "--noservers" = { 31 | set_if = "$haproxy_noservers$" 32 | description = "Disable checks for the servers in HAProxy (that they haven't reached the limits for the sessions or for queues)." 33 | } 34 | "--overrides" = { 35 | value = {{ 36 | return macro("$haproxy_overrides$").split(" ") 37 | }} 38 | description = "Override the defaults for a particular frontend or backend, in the form {name}:{override}, where {override} is the same format as --defaults above." 39 | } 40 | "--socket" = { 41 | value = "$haproxy_socket$" 42 | description = "Path to the socket check_haproxy should connect to" 43 | required = true 44 | } 45 | } 46 | } 47 | 48 | --------------------------------------------------------------------------------