Skip to content
Snippets Groups Projects
index.php 11.95 KiB
<?php
/**
 * IML APPMONITOR CHECKS FOR CI SERVER
 * 
 * @author: Axel Hahn
 * 
 * ------------------------------------------------------------------
 * 2014-10-24  v0.1
 * ...
 * 2024-09-04  php8 only: short array syntax
 */
require_once('classes/appmonitor-client.class.php');


require_once(__DIR__.'/../deployment/classes/project.class.php');
$oMonitor = new appmonitor();

$sCfgfile='../../config/inc_projects_config.php';
require_once $sCfgfile;

include('general_include.php');


// ----------------------------------------------------------------------
// config files
// ----------------------------------------------------------------------

$oMonitor->addCheck(
    [
        "name" => "read config file",
        "description" => "Check if config file is readable",
        "check" => [
            "function" => "File",
            "params" => [
                "filename" => $sCfgfile,
                "file"     => true,
                "readable" => true,
            ],
        ],
    ]
);



foreach ([
    "config/config_custom.php",
    "config/config_defaults.php",
    "config/inc_roles.php",
] as $sCfgfile) {

    $oMonitor->addCheck(
        [
            "name" => "read config file $sCfgfile",
            "description" => "Check if config file $sCfgfile is an existing file and is readable",
            "check" => [
                "function" => "File",
                "params" => [
                    "filename" => "../../$sCfgfile",
                    "file"     => true,
                    "readable" => true,
                ],
            ],
        ]
    );
}


// echo '<pre>' . print_r($aConfig, 1) . '</pre>';die();

// ----------------------------------------------------------------------
// directories
// ----------------------------------------------------------------------

foreach ([
    
    'tmpDir'=>['dir'=>$aConfig['tmpDir'], 'descr'=>'Temp Dir mit git Daten'],
    'configDir'=>['dir'=>$aConfig['configDir'], 'descr'=>'Ablage der Programm-Config'],
    'dataDir'=>['dir'=>$aConfig['dataDir'], 'descr'=>'Basisverzeichnis fue DB, Projekt-Configs, SSH-Keys'],
        'dataDir/database'=>['dir'=>$aConfig['dataDir'].'/database', 'descr'=>'DB-Ablage (Sqlite)'],
        'dataDir/projects'=>['dir'=>$aConfig['dataDir'].'/projects', 'descr'=>'Projekt-Configdateien'],
        'dataDir/sshkeys'=>['dir'=>$aConfig['dataDir'].'/sshkeys', 'descr'=>'SSH Keys'],
    
    'buildDir'=>['dir'=>$aConfig['buildDir'], 'descr'=>'Basisverzeichnis fuer Builds'],
    'packageDir'=>['dir'=>$aConfig['packageDir'], 'descr'=>'Basisverzeichnis der Pakete und Versionen'],
    'archiveDir'=>['dir'=>$aConfig['archiveDir'], 'descr'=>'Ablage der gebuildeten Archive'],
    
] as $sKey=>$aItem) {
    $oMonitor->addCheck(
        [
            "name" => "dir $sKey",
            "description" => $aItem['descr'],
            "parent" => "read config file",
            "check" => [
                "function" => "File",
                "params" => [
                    "filename" => $aItem['dir'],
                    "dir"      => true,
                    "writable" => true,
                ],
            ],
        ]
    );
}
$oMonitor->addCheck(
	[
		"name" => "Free space in Archive dir ",
		"description" => "The file storage must have some space left",
		"check" => [
			"function" => "Diskfree",
            "parent" => "read config file",
			"params" => [
				"directory" => $aConfig['archiveDir'],
				"warning"   => "2GB",
				"critical"  => "500MB",
            ],
		],
    ]
);
// ----------------------------------------------------------------------
// count of Projects
// ----------------------------------------------------------------------
$oPrj=new project();
$iProjectCount=count($oPrj->getProjects());
$iInProgress=0;
$iInQueue=0;
foreach ($oPrj->getProjects() as $sPrj) {
        $oPrj2=new project($sPrj);
        $aProgress=$oPrj2->getProgress();
        /*
         * so ist irgendwas anders ... er initialisiert das Projekt nicht
         * 
        $oPrj->setProjectById($sPrj);
        $aProgress=$oPrj->getProgress();
         */
        $iInProgress+=$aProgress['inprogress'] ? 1 : 0;
        $iInQueue+=$aProgress['hasQueue'] ? 1 : 0;
}
$oMonitor->addCheck(
    [
        "name" => "ci projects",
        "description" => "Count of Projects in CI Webgui",
        "group" => "monitor",
        "parent" => "read config file",
        "check" => [
            "function" => "Simple",
            "params" => [
                "result" => $iProjectCount ? RESULT_OK : RESULT_ERROR,
                "value" => "found projects: $iProjectCount",
                "count" => $iProjectCount,
                "visual" => "simple",
            ],
		],
    ]
);
$oMonitor->addCheck(
    [
        "name" => "ci inProgress",
        "description" => "Projects in progress",
        "group" => "monitor",
        "parent" => "read config file",
        "check" => [
            "function" => "Simple",
            "params" => [
                "result" => RESULT_OK,
                "value" => "found projects: $iInProgress",
                "count" => $iInProgress,
                "visual" => "simple",
            ],
		],
    ]
);
$oMonitor->addCheck(
    [
        "name" => "ci hasqueue",
        "description" => "Waiting for install",
        "group" => "monitor",
        "parent" => "read config file",
        "check" => [
            "function" => "Simple",
            "params" => [
                "result" => RESULT_OK,
                "value" => "found projects: $iInQueue",
                "count" => $iInQueue,
                "visual" => "simple",
            ],
		],
    ]
);

include 'plugins/apps/shared_check_ssl.php';


// ----------------------------------------------------------------------
// foreman
// ----------------------------------------------------------------------

if(isset($aConfig['foreman']['api'])){
    
    require_once(__DIR__.'/../deployment/classes/foremanapi.class.php');
    $oForeman = new ForemanApi($aConfig['foreman']);

    foreach (['hostgroups', 'hosts'] as $sForemanKey){
        $aFData=$oForeman->read([
            'request' => [
                [$sForemanKey],
            ],
            'response' => [
                'id', 'title'
            ],
        ]);
        
        $oMonitor->addCheck(
            [
                "name" => "Foreman $sForemanKey",
                "description" => "Count of Foreman $sForemanKey",
                "group" => "monitor",
                "parent" => "read config file",
                "check" => [
                    "function" => "Simple",
                    "params" => [
                        "result" => count($aFData) ? RESULT_OK : RESULT_ERROR,
                        "value" => "Count of found Foreman $sForemanKey: " . count($aFData)."; response status: ".$oForeman->getResponseStatus() . "; Http-Code ".$oForeman->getResponseInfo('http_code'),
                        "count" => count($aFData),
                        "visual" => "simple",
                    ],
                ],
            ]
        );
    }
}

// #6421 - add check for AWX
// #7709 - update for instances per rollout plugin
if(isset($aConfig['plugins']['rollout']['awx'])){
    $aOpts=[];
    foreach ($aConfig['plugins']['rollout'] as $sId => $aRolloutConfig) {
        if ($aRolloutConfig['plugin'] == "awx") {
            $aOpts['url'] = $aRolloutConfig['url'].'/';
            $aOpts['userpwd'] = $aRolloutConfig['user']
            . (isset($aRolloutConfig['password'])
                ? ':'.$aRolloutConfig['password']
                : ''
            )
            ;

            $oMonitor->addCheck(
                [
                    "name" => "AWX API - ".$sId,
                    "description" => "check if AWX api is available",
                    "group" => "network",
                    "parent" => "read config file",
                    "check" => [
                        "function" => "HttpContent",
                        "params" => $aOpts,
                    ],
                ]
            );
        }
    }
}

// ----------------------------------------------------------------------
// ssh targets
// ----------------------------------------------------------------------

if(count($aConfig['mirrorPackages'])){
    foreach($aConfig['mirrorPackages'] as $sHostKey=>$aData){
        /*
        [puppet.one] => Array
        (
            [type] => rsync
            [runas] => 
            [target] => copy-deployment@puppet:/var/shared/imldeployment
        )
         */
        $sTargetHost = preg_replace('/\:.*/', '', $aData['target']);
        $sCmd='ssh '.$sTargetHost. ' ls -1';

        exec($sCmd, $sOut, $iRc);
        $oMonitor->addCheck(
            [
                "name" => "mirror target $sHostKey",
                "description" => "Sync target of generated packages",
                "group" => "network",
                "parent" => "read config file",
                "check" => [
                    "function" => "Simple",
                    "params" => [
                        "result" => $iRc ? RESULT_ERROR : RESULT_OK,
                        "value" => "Command [$sCmd] returns with exitcode $iRc; output: ".implode(" ", $sOut),
                    ],
                ],
            ]
        );
    }
    
}
foreach([
    /*
    [
        'host'=>'gitlab.iml.unibe.ch',
        'port'=>22,
        'descr'=>'SSH port to Gitlab on gitlab.iml.unibe.ch'
    ],
     */
    [
        'host'=>'git-repo.iml.unibe.ch',
        'port'=>22,
        'descr'=>'SSH port to Gitlab on git-repo.iml.unibe.ch'
    ],
    [
        'host'=>'github.com',
        'port'=>22,
        'descr'=>'SSH port to Github'
    ],
] as $aDescr){
    $oMonitor->addCheck(
        [
            "name" => 'port check '.$aDescr['host'].':'.$aDescr['port'],
            "description" => $aDescr['descr'],
            "group" => "cloud",
            "parent" => "read config file",
            "check" => [
                "function" => "PortTcp",
                "params" => [
                    "port"=>$aDescr['port'],
                    "host"=>$aDescr['host'],
                ],
            ],
        ]
    );
}


// ----------------------------------------------------------------------
// database
// ----------------------------------------------------------------------

$sSqlitefile=$aConfig['dataDir'].'/database/logs.db';
$oMonitor->addCheck(
    [
        "name" => "Sqlite DB for action logs",
        "description" => "Connect sqlite db ". basename($sSqlitefile),
        "parent" => "read config file",
        "check" => [
            "function" => "SqliteConnect",
            "params" => [
                "db"=>$sSqlitefile
            ],
        ],
    ]
);

// ----------------------------------------------------------------------
// system stuff
// ----------------------------------------------------------------------
$oMonitor->addCheck(
    [
        "name" => "plugin Load",
        "description" => "current load",
        "parent" => false,
        "check" => [
            "function" => "Loadmeter",
            "params" => [
                "warning" => 1.0,
                "error" => 3,
            ],
        ],
        "worstresult" => RESULT_OK
    ]
);
$oMonitor->addCheck(
    [
        "name" => "plugin ApacheProcesses",
        "description" => "Apache processes",
        "parent" => false,
        "check" => [
            "function" => "ApacheProcesses",
            "params" => [
                "warning" => 50,
                "error" => 75,
            ],
        ],
        "worstresult" => RESULT_OK
    ]
);

// ----------------------------------------------------------------------

$oMonitor->setResult();
$oMonitor->render();

// ----------------------------------------------------------------------