WebsiteStatusCheck.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/perl -w
  2. # Klimbim Software collection, A bag full of things
  3. # Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # 2012 by Johannes 'Banana' Keßler
  18. # this file can be used as a "early morning check"
  19. use warnings;
  20. use strict;
  21. use LWP::UserAgent;
  22. use HTTP::Request;
  23. use utf8;
  24. use Term::ANSIColor qw(:constants);
  25. my @urlsToCheck = ('http://url.tld','http://another.url.tld');
  26. my $localtime = localtime;
  27. my ($day,$month,$date,$hour,$year) = split /\s+/,scalar localtime;
  28. my $response_limit = 12; #seconds
  29. print "\n+" .('-' x 94) . "+\n";
  30. print "|", ' ' x 30,"Time: $hour",' ' x 50,"|\n";
  31. print "|",' 'x 10,'HOST',' ' x 37,'STATUS',' ' x 7, "RESPONSE",,' ' x 22,"|\n";
  32. print "+" .('-' x 94) . "+\n";
  33. # basich url access check
  34. foreach my $url(@urlsToCheck) {
  35. # ordinary response check
  36. check_url($url);
  37. # special check which retuns the output.
  38. # eg. if you want to check the installed version. You could query the version info and display it
  39. check_url($url.'/_path/to/special/file/',"1");
  40. }
  41. print "+" .('-' x 94) . "+\n";
  42. print "\nProcess FINISH\n";
  43. #
  44. # basic url check
  45. #
  46. sub check_url { # subroutine who check given URL
  47. my ($target,$printResponse) = @_;
  48. my $ua = LWP::UserAgent->new;
  49. $ua->agent("$0/0.1 " . $ua->agent);
  50. #$ua->proxy(['http'], 'http://10.0.0.1:80/'); # proxy if you need one
  51. my $req = HTTP::Request->new(GET => "$target");
  52. $req->header('Accept' => 'text/html');
  53. # send request
  54. my $start = time;
  55. my $res = $ua->request($req);
  56. # debug messages
  57. # print $res->status_line."\n";
  58. # print $res->content."\n";
  59. # check the outcome
  60. if ($res->is_success) {
  61. # Success....all content of page has been received
  62. my $time = time;
  63. my $out_format;
  64. $time = ($time - $start); # Result of timer
  65. if ($response_limit && ($response_limit <= $time)) {
  66. $out_format = sprintf "| %-50.50s %-10s %-30s |\n", $target, "SLOW", "Response $time seconds";
  67. print CLEAR, YELLOW, $out_format, RESET;
  68. } else {
  69. if($printResponse && $printResponse eq "1") { # this makes only sense if we have a success.
  70. $out_format = sprintf "| %-50.50s %-10s %-30s |\n", $target, "RESPONSE", trim($res->content);
  71. print CLEAR,GREEN, $out_format, RESET;
  72. }
  73. else {
  74. $out_format = sprintf "| %-50.50s %-10s %-30s |\n", $target, "ACCESSED", "Response $time seconds";
  75. print CLEAR,GREEN, $out_format, RESET;
  76. # check if we have something we want in the response
  77. # this can be used to "validate" the response, seince a 202 can also en empty page...
  78. if($res->content =~ /someTextStringToCheck/) {
  79. my $out_format = sprintf "| %-50.50s %-10s %-30s |\n", '', "OK", "Content check ok";
  80. print CLEAR, GREEN, $out_format, RESET;
  81. }
  82. else {
  83. my $out_format = sprintf "| %-50.50s %-10s %-30s |\n", '', "FALSE", "Missing content ?";
  84. print BOLD, RED, $out_format, RESET;
  85. }
  86. }
  87. }
  88. } else { # Error .... Site is DOWN
  89. my $out_format = sprintf "| %-50.50s %-10s %-30s |\n", $target, "DOWN", " N/A";
  90. print BOLD, RED, $out_format, RESET;
  91. }
  92. }
  93. sub trim() {
  94. my $string = shift;
  95. $string =~ s/^\s+//;
  96. $string =~ s/\s+$//;
  97. return $string;
  98. }
  99. # end file