oddmuse_stats 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/perl -w
  2. # -*- perl -*-
  3. =head1 NAME
  4. oddmuse-stats - Plugin to monitor Oddmuse edits
  5. =head1 CONFIGURATION
  6. Set env.parent_dirs in the config file. The directories in this list
  7. are searched for data directories containing rc.log files. No
  8. whitespace in the directory names, sorry.
  9. Example:
  10. [oddmuse_stats]
  11. user www-data
  12. env.parent_dirs /home/alex /home/alex/campaignwiki
  13. =head1 AUTHORS
  14. Original Author: Alex Schroeder
  15. =head1 LICENSE
  16. GPLv3
  17. =head1 MAGIC MARKERS
  18. #%# family=auto
  19. #%# capabilities=autoconf
  20. =cut
  21. use Munin::Plugin;
  22. use File::Basename;
  23. # The wiki directories may not contain any spaces.
  24. # Use the config file to set the environment variable!
  25. my @parent_dirs = ();
  26. my %logfiles = ();
  27. my %names = ();
  28. my $debug = $ENV{MUNIN_DEBUG};
  29. if ($ENV{'parent_dirs'}) {
  30. @parent_dirs = split(/ /, $ENV{'parent_dirs'});
  31. } else {
  32. die "The parent_dirs environment variable must be set.\n";
  33. }
  34. for my $parent_dir (@parent_dirs) {
  35. warn "opening $parent_dir\n" if $debug;
  36. if (opendir(my $dh, $parent_dir)) {
  37. while(readdir $dh) {
  38. next if $_ eq '.' or $_ eq '..';
  39. if (-r "$parent_dir/$_/rc.log") {
  40. my $basename = basename($_);
  41. $names{clean_fieldname($basename)}
  42. = $basename;
  43. $logfiles{clean_fieldname($basename)}
  44. = "$parent_dir/$_/rc.log";
  45. } else {
  46. warn "discarding $_\n" if $debug;
  47. }
  48. }
  49. closedir $dh;
  50. }
  51. }
  52. my $yesterday = time() - 86400;
  53. if ($ARGV[0]) {
  54. if ($ARGV[0] eq 'autoconf') {
  55. if (keys %logfiles) {
  56. print "yes\n";
  57. exit 0;
  58. } else {
  59. print "no (no logfiles found in " . join(", ", @parent_dirs) . ")\n";
  60. exit 0;
  61. }
  62. } elsif ($ARGV[0] eq 'config') {
  63. print "graph_title Oddmuse Wikis\n";
  64. print "graph_category wikis\n";
  65. print "graph_info This graph shows how many edits the wiki had in the last 24h.\n";
  66. print "graph_vlabel edits/day\n";
  67. print "graph_order";
  68. for my $wiki (sort keys %logfiles) {
  69. print " $wiki";
  70. };
  71. print "\n";
  72. for my $wiki (sort keys %logfiles) {
  73. my $name = $names{$wiki};
  74. print "$wiki.label $name\n";
  75. }
  76. exit 0;
  77. }
  78. }
  79. for my $wiki (sort keys %logfiles) {
  80. open (my $fh, '<', $logfiles{$wiki})
  81. or die "cannot open " . $logfiles{$wiki} . ": $!";
  82. my $value = 0;
  83. while (<$fh>) {
  84. my ($ts) = split(/\x1e/);
  85. $value++ if $ts and $ts >= $yesterday;
  86. }
  87. print "$wiki.value $value\n";
  88. }