Issues (48)

library/Trapdirector/TrapsProcess/TrapApi.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Trapdirector;
4
5
use Icinga\Module\Trapdirector\Icinga2API;
6
use Exception;
7
use stdClass as stdClass;
8
9
/**
10
 * Distributed status & API calls 
11
 * 
12
 * @license GPL
13
 * @author Patrick Proy
14
 * @package Trapdirector
15
 * @subpackage Processing
16
 */
17
class TrapApi
18
{
19
20
    // Constants
21
    public const MASTER=1;
22
    public const MASTERHA=2;
23
    public const SAT=3;
24
    public $stateArray = array('MASTER' => TrapApi::MASTER, 'MASTERHA' => TrapApi::MASTERHA , 'SAT' => TrapApi::SAT  );
25
    
26
    /** @var integer $whoami current server : MASTER MASTERHA or SAT */
27
    public $whoami = TrapApi::MASTER;
28
    /** @var string $masterIP ip of master if MASTERHA or SAT  */
29
    public $masterIP='';
30
    /** @var integer $masterPort port of master if MASTERHA or SAT  */
31
    public $masterPort=443;
32
    /** @var string $masterUser user to log in API  */
33
    public $masterUser='';
34
    /** @var string $masterPass password */
35
    public $masterPass='';
36
    
37
    /** @var Logging $logging logging class */
38
    protected $logging;
39
    
40
    /**
41
     * Create TrapApi class
42
     * @param Logging $logClass
43
     */
44
    function __construct($logClass)
45
    {
46
        $this->logging=$logClass;
47
    }
48
49
    /**
50
     * Return true if ode is master.
51
     * @return boolean
52
     */
53
    public function isMaster()
54
    {
55
        return ($this->whoami == MASTER);
0 ignored issues
show
The constant Trapdirector\MASTER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
    }
57
58
    /**
59
     * return status of node
60
     * @return number
61
     */
62
    public function getStatus()
63
    {
64
        return $this->whoami;    
65
    }
66
    
67
    /**
68
     * Set status os node to $status
69
     * @param string $status
70
     * @return boolean : true if $status is correct, or false.
71
     */
72
    public function setStatus(string $status)
73
    {
74
        if (! isset($this->stateArray[$status]))
75
        {
76
            return FALSE;
77
        }
78
        
79
        $this->logging->log('Setting status to : ' . $status, INFO);
80
        
81
        $this->whoami = $this->stateArray[$status];
82
        
83
        return TRUE;
84
    }
85
 
86
    public function setStatusMaster()
87
    {
88
        $this->whoami = TrapApi::MASTER;
89
    }
90
    
91
    /**
92
     * Set params for API connection
93
     * @param string $IP
94
     * @param int $port
95
     * @param string $user
96
     * @param string $pass
97
     * @return boolean true if params are OK
98
     */
99
    public function setParams(string $IP, int $port, string $user, string $pass)
100
    {
101
        $this->masterIP = $IP;
102
        $this->masterPort = $port;
103
        $this->masterUser = $user;
104
        $this->masterPass = $pass;
105
        
106
        return true;
107
    }
108
    
109
}