Add separate commands to generate a munin plugin and to provide munin data; split aggegrate and count login data

master v0.1.0
Lars Vierbergen 7 years ago
parent 82b0dba9d5
commit 94262f0377
  1. 38
      Command/DumpStatsCommand.php
  2. 81
      Command/GenerateMuninCommand.php
  3. 66
      Command/MuninStatsCommand.php
  4. 66
      EventListener/LoginStatsListener.php

@ -31,9 +31,7 @@ class DumpStatsCommand extends Command
protected function configure()
{
$this->setName('stats:dump')
->addOption('module', 'm', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Modules to fetch statistics for')
->addOption('munin', null, InputOption::VALUE_OPTIONAL, 'Output data for consumption by munin', false);
$this->setName('stats:dump')->addOption('module', 'm', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Modules to fetch statistics for');
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -48,29 +46,17 @@ class DumpStatsCommand extends Command
$event = new StatsEvent($input->getOption('module'));
$eventDispatcher->dispatch(StatsEvent::class, $event);
if ($input->getOption('munin') !== false) {
foreach ($event->getModules() as $module) {
$output->writeln('multigraph authserver_' . $module, OutputInterface::OUTPUT_RAW);
if ($input->getOption('munin') === 'config') {
$stats = $event->getMuninConfig($module);
$suffix = ' ';
} else {
$stats = $event->getModuleStatistics($module);
$suffix = '.value ';
}
foreach ($stats as $k => $v) {
$output->writeln($k . $suffix . $v, OutputInterface::OUTPUT_RAW);
}
}
} else {
$table = new Table($output);
foreach ($event->getStatistics() as $name => $value) {
$table->addRow([
$name,
$value
]);
}
$table->render();
$table = new Table($output);
$table->setHeaders([
'Statistic',
'Value'
]);
foreach ($event->getStatistics() as $name => $value) {
$table->addRow([
$name,
$value
]);
}
$table->render();
}
}

@ -0,0 +1,81 @@
<?php
/**
* Authserver, an OAuth2-based single-signon authentication provider written in PHP.
*
* Copyright (C) 2017 Lars Vierbergen
*
* his program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace vierbergenlars\AuthserverStatsBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use vierbergenlars\AuthserverStatsBundle\Event\StatsEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Process\PhpExecutableFinder;
class GenerateMuninCommand extends Command
{
protected function configure()
{
$this->setName('stats:generate:munin-plugin')
->addOption('module', 'm', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Modules to generate munin plugin for')
->addOption('exclude', 'x', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Modules to exclude from munin graphing')
->addOption('static-config', 's', InputOption::VALUE_NONE, 'Include a statically generated munin configuration');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$eventDispatcher = $this->getApplication()
->getKernel()
->getContainer()
->get('event_dispatcher');
/* @var $eventDispatcher Symfony\Component\EventDispatcher\EventDispatcher */
$event = new StatsEvent($input->getOption('module'));
$eventDispatcher->dispatch(StatsEvent::class, $event);
$modules = array_merge(array_diff($event->getModules(), $input->getOption('exclude')), $input->getOption('module'));
$output->writeln('#!/bin/sh');
if ($input->getOption('static-config')) {
$output->writeln([
'case $1 in',
'config)',
'cat <<\'EOM\''
]);
foreach ($modules as $module) {
$muninConfig = $event->getMuninConfig($module);
foreach ($muninConfig as $key => $value) {
$output->writeln($key . ' ' . $value, OutputInterface::OUTPUT_RAW);
}
}
$output->writeln([
'EOM',
'exit 0;;',
'esac'
]);
}
$modulesParameters = array_map(function ($module) {
return '-m ' . $module;
}, $modules);
$phpFinder = new PhpExecutableFinder();
$kernel = $this->getApplication()->getKernel();
/* @var $kernel \AppKernel */
$output->writeln(sprintf('%s %s -e %s stats:munin "$1" %s', $phpFinder->find(), $kernel->getRootDir() . '/console', $kernel->getEnvironment(), implode(' ', $modulesParameters)));
}
}

@ -0,0 +1,66 @@
<?php
/**
* Authserver, an OAuth2-based single-signon authentication provider written in PHP.
*
* Copyright (C) 2017 Lars Vierbergen
*
* his program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace vierbergenlars\AuthserverStatsBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use vierbergenlars\AuthserverStatsBundle\Event\StatsEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
class MuninStatsCommand extends Command
{
protected function configure()
{
$this->setName('stats:munin')
->addOption('module', 'm', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Modules to fetch statistics for')
->addArgument('munin', InputArgument::OPTIONAL, 'munin arguments');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$eventDispatcher = $this->getApplication()
->getKernel()
->getContainer()
->get('event_dispatcher');
/* @var $eventDispatcher Symfony\Component\EventDispatcher\EventDispatcher */
$event = new StatsEvent($input->getOption('module'));
$eventDispatcher->dispatch(StatsEvent::class, $event);
foreach ($event->getModules() as $module) {
$output->writeln('multigraph authserver_' . $module, OutputInterface::OUTPUT_RAW);
if ($input->getArgument('munin') === 'config') {
$stats = $event->getMuninConfig($module);
$suffix = ' ';
} else {
$stats = $event->getModuleStatistics($module);
$suffix = '.value ';
}
foreach ($stats as $k => $v) {
$output->writeln($k . $suffix . $v, OutputInterface::OUTPUT_RAW);
}
}
}
}

@ -64,38 +64,50 @@ class LoginStatsListener implements EventSubscriberInterface
public function getLoginStats(StatsEvent $event)
{
if (!$event->isEnabled('login'))
return;
if ($event->isEnabled('login')) {
$event->setMuninConfig('login', [
'graph_title' => 'Authserver logins',
'graph_vlabel' => 'Number of logins',
'graph_category' => 'authserver',
'5min.label' => 'Logins in past 5 minutes',
'1hour.label' => 'Logins in past hour',
'1day.label' => 'Logins in past day',
'1week.label' => 'Logins in past week',
'total.label' => 'Logins per ${graph_period}',
'total.type' => 'COUNTER'
]);
$queryBuilder = $this->registry->getRepository(LoginEntry::class)->createQueryBuilder('e');
/* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
$event->setMuninConfig('login', [
'graph_title' => 'Authserver logins',
'graph_vlabel' => 'Number of logins',
'graph_category' => 'authserver',
'graph_period' => 'minute',
'total.label' => 'Logins per ${graph_period}',
'total.type' => 'COUNTER'
]);
$queryBuilder->select('count(e)')->where('e.loginTime > :time');
$queryBuilder = $this->registry->getRepository(LoginEntry::class)->createQueryBuilder('e');
/* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
foreach ([
'5min',
'1hour',
'1day',
'1week'
] as $timeAgo) {
$event->addStatistic('login.' . $timeAgo, $queryBuilder->getQuery()
->setParameter('time', new \DateTime($timeAgo . ' ago'))
$event->addStatistic('login.total', $queryBuilder->select('count(e)')
->getQuery()
->getSingleScalarResult());
}
$event->addStatistic('login.total', $queryBuilder->resetDQLPart('where')
->getQuery()
->getSingleScalarResult());
if ($event->isEnabled('login_aggregate')) {
$event->setMuninConfig('login_aggregate', [
'graph_title' => 'Authserver logins',
'graph_vlabel' => 'Number of logins',
'graph_category' => 'authserver',
'5min.label' => 'Logins in past 5 minutes',
'1hour.label' => 'Logins in past hour',
'1day.label' => 'Logins in past day',
'1week.label' => 'Logins in past week'
]);
$queryBuilder = $this->registry->getRepository(LoginEntry::class)->createQueryBuilder('e');
/* @var $queryBuilder \Doctrine\ORM\QueryBuilder */
$queryBuilder->select('count(e)')->where('e.loginTime > :time');
foreach ([
'5min',
'1hour',
'1day',
'1week'
] as $timeAgo) {
$event->addStatistic('login_aggregate.' . $timeAgo, $queryBuilder->getQuery()
->setParameter('time', new \DateTime($timeAgo . ' ago'))
->getSingleScalarResult());
}
}
}
}