1
0

ShowStatPlugin.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package Foswiki::Plugins::ShowStatPlugin;
  2. # Always use strict to enforce variable scoping
  3. use strict;
  4. use utf8;
  5. use DBI;
  6. use Data::Dumper;
  7. # $VERSION is referred to by Foswiki, and is the only global variable that
  8. # *must* exist in this package
  9. use vars qw( $VERSION $RELEASE $debug $dbstat $dbrstat $dbcacti $pluginName );
  10. use Foswiki::Func (); # The plugins API
  11. use Foswiki::Plugins (); # For the API version
  12. # This should always be $Rev: 8713$ so that Foswiki can determine the checked-in
  13. # status of the plugin. It is used by the build automation tools, so
  14. # you should leave it alone.
  15. $VERSION = '$Rev: 8713$';
  16. # This is a free-form string you can use to "name" your own plugin version.
  17. # It is *not* used by the build automation tools, but is reported as part
  18. # of the version number in PLUGINDESCRIPTIONS.
  19. $RELEASE = '1.01';
  20. # Name of this Plugin, only used in this module
  21. $pluginName = 'ShowStatPlugin';
  22. =pod
  23. ---++ initPlugin($topic, $web, $user, $installWeb) -> $boolean
  24. * =$topic= - the name of the topic in the current CGI query
  25. * =$web= - the name of the web in the current CGI query
  26. * =$user= - the login name of the user
  27. * =$installWeb= - the name of the web the plugin is installed in
  28. REQUIRED
  29. Called to initialise the plugin. If everything is OK, should return
  30. a non-zero value. On non-fatal failure, should write a message
  31. using Foswiki::Func::writeWarning and return 0. In this case
  32. %FAILEDPLUGINS% will indicate which plugins failed.
  33. In the case of a catastrophic failure that will prevent the whole
  34. installation from working safely, this handler may use 'die', which
  35. will be trapped and reported in the browser.
  36. You may also call =Foswiki::Func::registerTagHandler= here to register
  37. a function to handle tags that have standard Foswiki syntax - for example,
  38. =%MYTAG{"my param" myarg="My Arg"}%. You can also override internal
  39. Foswiki tag handling functions this way, though this practice is unsupported
  40. and highly dangerous!
  41. =cut
  42. sub initPlugin {
  43. my( $topic, $web ) = @_;
  44. # check for Plugins.pm versions
  45. if( $Foswiki::Plugins::VERSION < 1.026 ) {
  46. Foswiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
  47. return 0;
  48. }
  49. return 0 unless $Foswiki::cfg{Plugins}{DatabasePlugin}{ConfigSource};
  50. if ( $Foswiki::cfg{Plugins}{DatabasePlugin}{ConfigSource} eq 'Local' ) {
  51. foreach my $info ( @{ $Foswiki::cfg{Plugins}{DatabasePlugin}{Databases} } ) {
  52. if ($info->{description} eq "stat") { $dbstat = $info; next; }
  53. if ($info->{description} eq "rstat") { $dbrstat = $info; next; }
  54. if ($info->{description} eq "cacti") { $dbcacti = $info; next; }
  55. }
  56. }
  57. return 0 if (!$dbstat or !$dbrstat);
  58. # register the _EXAMPLETAG function to handle %EXAMPLETAG{...}%
  59. Foswiki::Func::registerTagHandler( 'SHOWSTAT', \&_ShowStat );
  60. # Plugin correctly initialized
  61. return 1;
  62. }
  63. sub StrToIp{
  64. return unpack('N',pack('C4',split(/\./,$_[0])));
  65. }
  66. sub _ShowStat {
  67. my($session, $params, $theTopic, $theWeb) = @_;
  68. ### parameters
  69. my $host = $params->{_DEFAULT} || $params->{host};
  70. return "" if (!$host);
  71. my $host_aton=StrToIp($host);
  72. #wait for statsync
  73. sleep(2);
  74. my $connect_options = "dbi:$dbstat->{driver}:database=$dbstat->{database};host=$dbstat->{hostname}";
  75. my $connect_user = "$dbstat->{username}";
  76. my $connect_password = "$dbstat->{password}";
  77. if ($theWeb=~/Internet/) {
  78. $connect_options = "dbi:$dbrstat->{driver}:database=$dbrstat->{database};host=$dbrstat->{hostname}";
  79. $connect_user = "$dbrstat->{username}";
  80. $connect_password = "$dbrstat->{password}";
  81. }
  82. my $dbh = DBI->connect($connect_options,$connect_user,$connect_password);
  83. my $SQL = "SELECT A.id, A.ip, A.mac, L.login, A.nagios, A.dhcp_hostname, A.enabled, G.group_name, Q.queue_name,
  84. A.last_found, A.comments FROM User_auth as A, User_list as L, Group_list as G, Queue_list As Q
  85. WHERE A.user_id = L.id and A.filter_group_id = G.id and Q.id = A.queue_id AND A.deleted =0 and A.ip_int=".$host_aton." LIMIT 1";
  86. my $status = '';
  87. eval {
  88. if ( !defined $dbh ) { return "Cannot connect to mySQL server: $DBI::errstr\n"; }
  89. $dbh->do('SET NAMES utf8mb4');
  90. $dbh->{'mysql_enable_utf8'} = 1;
  91. my $sth = $dbh->prepare($SQL);
  92. $sth->execute;
  93. my $res = $sth->fetchrow_hashref();
  94. if ($res) {
  95. my $cSQL = "SELECT * FROM `config` WHERE option_id=";
  96. my $sth1 = $dbh->prepare($cSQL."57");
  97. $sth1->execute;
  98. my $nagios_row = $sth1->fetchrow_hashref();
  99. my $nagios_url;
  100. if ($nagios_row) { $nagios_url = $nagios_row->{value}."/cgi-bin/status.cgi?navbarsearch=1&host=".$host; }
  101. $sth1 = $dbh->prepare($cSQL."62");
  102. $sth1->execute;
  103. my $stat_row = $sth1->fetchrow_hashref();
  104. my $stat_url;
  105. if ($stat_row) { $stat_url = $stat_row->{value}."/admin/users/editauth.php?id=".$res->{id}; }
  106. $status ='<div style="float: right; width: 200px;">';
  107. $status.='%BLUE%STAT:%ENDCOLOR%<br>';
  108. $status.='id: '.$res->{id}.'<br>';
  109. if ($stat_url) {
  110. $status.='Login: <a href="'.$stat_url.'">'.$res->{login}.'</a><br>';
  111. } else { $status.='Login: '.$res->{login}.'<br>'; }
  112. if ($res->{enabled}) { $status.='Включен: Да<br>'; } else { $status.='Включен: Нет<br>'; }
  113. if ($res->{nagios}) {
  114. if ($nagios_url) {
  115. $status.='Nagios: <a href="'.$nagios_url.'">Да</a><br>';
  116. } else { $status.='Nagios: Да<br>'; }
  117. } else { $status.='Nagios: Нет<br>'; }
  118. $status.='Dhcp hostname: '.$res->{dhcp_hostname}.'<br>' if ($res->{dhcp_hostname});
  119. $status.='Фильтр: '.$res->{group_name}.'<br>';
  120. $status.='Шейпер: '.$res->{queue_name}.'<br>';
  121. $status.='Комментарий: '.$res->{comments}.'<br>';
  122. $status.='last found: '.$res->{last_found}.'<br>' if ($res->{last_found});
  123. $status.='</div>';
  124. $status.='</div>';
  125. $status.='<div style="clear: left;"><p></p></div>';
  126. }
  127. };
  128. if ($@) { return "DBI error: $@"; }
  129. return $status;
  130. }
  131. 1;