Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Imldeployment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IML Open Source
Imldeployment
Commits
29df4d11
Commit
29df4d11
authored
6 years ago
by
hahn
Browse files
Options
Downloads
Patches
Plain Diff
public_html/appmonitor update appmonitor client and added checks
parent
e1202aa0
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
public_html/appmonitor/appmonitor-checks.class.php
+0
-343
0 additions, 343 deletions
public_html/appmonitor/appmonitor-checks.class.php
public_html/appmonitor/appmonitor-client.class.php
+0
-289
0 additions, 289 deletions
public_html/appmonitor/appmonitor-client.class.php
with
0 additions
and
632 deletions
public_html/appmonitor/appmonitor-checks.class.php
deleted
100644 → 0
+
0
−
343
View file @
e1202aa0
<?php
define
(
"RESULT_OK"
,
0
);
define
(
"RESULT_UNKNOWN"
,
1
);
define
(
"RESULT_WARNING"
,
2
);
define
(
"RESULT_ERROR"
,
3
);
/**
* APPMONITOR CLIENT CHECKS<br>
* <br>
* THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE <br>
* LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR <br>
* OTHER PARTIES PROVIDE THE PROGRAM ?AS IS? WITHOUT WARRANTY OF ANY KIND, <br>
* EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED <br>
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE <br>
* ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. <br>
* SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY <br>
* SERVICING, REPAIR OR CORRECTION.<br>
* <br>
* --------------------------------------------------------------------------------<br>
* <br>
* --- HISTORY:<br>
* 2014-10-24 0.5 axel.hahn@iml.unibe.ch<br>
* 2015-04-08 0.9 axel.hahn@iml.unibe.ch added sochket test: checkPortTcp<br>
* --------------------------------------------------------------------------------<br>
* @version 0.9
* @author Axel Hahn
* @link TODO
* @license GPL
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL 3.0
* @package IML-Appmonitor
*/
class
appmonitorcheck
{
// ----------------------------------------------------------------------
// CONFIG
// ----------------------------------------------------------------------
/**
* config container
* @var array
*/
private
$_aConfig
=
array
();
/**
* data of all checks
* @var array
*/
private
$_aData
=
array
();
// ----------------------------------------------------------------------
// CONSTRUCTOR
// ----------------------------------------------------------------------
/**
* constructor (nothing)
*/
public
function
__construct
()
{
}
// ----------------------------------------------------------------------
// PRIVATE FUNCTIONS
// ----------------------------------------------------------------------
/**
* create basic array values for metadata
* @return boolean
*/
private
function
_createDefaultMetadata
()
{
$this
->
_aData
=
array
(
"name"
=>
$this
->
_aConfig
[
"name"
],
"description"
=>
$this
->
_aConfig
[
"description"
],
"result"
=>
RESULT_UNKNOWN
,
"value"
=>
false
,
);
return
true
;
}
/**
* set a result value of a check
* @param type $iResult
* @return type
*/
private
function
_setResult
(
$iResult
)
{
return
$this
->
_aData
[
"result"
]
=
(
int
)
$iResult
;
}
/**
* set a result value of a check
* @param type $iResult
* @return type
*/
private
function
_setOutput
(
$s
)
{
return
$this
->
_aData
[
"value"
]
=
(
string
)
$s
;
}
/**
* set result and output
* @param type $iResult
* @param type $s
* @return boolean
*/
private
function
_setReturn
(
$iResult
,
$s
)
{
$this
->
_setResult
(
$iResult
);
$this
->
_setOutput
(
$s
);
return
true
;
}
private
function
_checkArrayKeys
(
$aConfig
,
$sKeyList
)
{
foreach
(
explode
(
","
,
$sKeyList
)
as
$sKey
)
{
if
(
!
array_key_exists
(
$sKey
,
$aConfig
))
{
die
(
'ERROR in '
.
__CLASS__
.
"<br>array requires the keys [
$sKeyList
] - but key '
$sKey
' was not found in config array <pre>"
.
print_r
(
$aConfig
,
true
));
}
if
(
is_null
(
$aConfig
[
$sKey
]))
{
die
(
'ERROR in '
.
__CLASS__
.
"<br> key '
$sKey
' is empty in config array <pre>"
.
print_r
(
$aConfig
,
true
));
}
}
return
true
;
}
// ----------------------------------------------------------------------
// PUBLIC FUNCTIONS
// ----------------------------------------------------------------------
/**
* perform a check
* @param type $aConfig
* Array
* (
* [name] => Dummy
* [description] => Dummy Test
* [check] => array(
* [function] => [check function] // i.e. Simple
* [params] => [array] // optional; arguments for Check function
* // its keys depend on the function
* )
* )
*
* @return array
*/
public
function
makeCheck
(
$aConfig
)
{
$this
->
_checkArrayKeys
(
$aConfig
,
"name,description,check"
);
$this
->
_checkArrayKeys
(
$aConfig
[
"check"
],
"function"
);
$this
->
_aConfig
=
$aConfig
;
$this
->
_createDefaultMetadata
();
$sCheck
=
"check"
.
$this
->
_aConfig
[
"check"
][
"function"
];
if
(
!
method_exists
(
$this
,
$sCheck
))
{
die
(
__CLASS__
.
" check not found:
$sCheck
<pre>"
.
print_r
(
$aConfig
,
true
));
}
$aParams
=
array_key_exists
(
"params"
,
$this
->
_aConfig
[
"check"
])
?
$this
->
_aConfig
[
"check"
][
"params"
]
:
array
();
// call the check ...
call_user_func
(
array
(
$this
,
$sCheck
),
$aParams
);
// echo "<pre>"; print_r($this->listChecks()); die();
// ... and send response
return
$this
->
respond
();
}
/**
* list all available check functions. This is a helper class you cann call
* to get an overview overbuilt in functions. You get a flat array with
* all function names.
* @return array
*/
public
function
listChecks
()
{
$aReturn
=
array
();
$class
=
new
ReflectionClass
(
$this
);
foreach
(
$class
->
getMethods
(
ReflectionMethod
::
IS_PRIVATE
)
as
$oReflectionMethod
)
{
if
(
strpos
(
$oReflectionMethod
->
name
,
"check"
)
===
0
)
{
$aReturn
[]
=
(
string
)
$oReflectionMethod
->
name
;
}
}
return
$aReturn
;
}
/**
* final call of class: send response (data array)
* @return type
*/
public
function
respond
()
{
return
$this
->
_aData
;
}
// ----------------------------------------------------------------------
// CHECK FUNCTIONS (private)
// ----------------------------------------------------------------------
/**
* most simple check: set values
* @return type
*/
private
function
checkSimple
(
$aParams
)
{
$aHelp
=
array
(
"result"
=>
"(integer) result value"
,
"value"
=>
"(string) explaination"
);
$this
->
_checkArrayKeys
(
$aParams
,
"result,value"
);
return
$this
->
_setReturn
((
int
)
$aParams
[
"result"
],
$aParams
[
"value"
]);
}
/**
* make http request and test response body
* @param array $aParams
* array(
* "url" url to fetch
* "contains" string that must exist in response body
* )
*/
private
function
checkHttpContent
(
$aParams
,
$iTimeout
=
5
)
{
$this
->
_checkArrayKeys
(
$aParams
,
"url,contains"
);
if
(
!
function_exists
(
"curl_init"
))
{
die
(
"ERROR: PHP CURL module is not installed."
);
}
$ch
=
curl_init
(
$aParams
[
"url"
]);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
1
);
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYPEER
,
0
);
curl_setopt
(
$ch
,
CURLOPT_TIMEOUT
,
$iTimeout
);
$res
=
curl_exec
(
$ch
);
curl_close
(
$ch
);
if
(
!
$res
)
{
$this
->
_setReturn
(
RESULT_ERROR
,
'ERROR: failed to fetch '
.
$aParams
[
"url"
]
.
'.'
);
}
else
{
if
(
!
strpos
(
$res
,
$aParams
[
"contains"
])
===
false
)
{
$this
->
_setReturn
(
RESULT_OK
,
'OK: The text "'
.
$aParams
[
"contains"
]
.
'" was found in response of '
.
$aParams
[
"url"
]
.
'.'
);
}
else
{
$this
->
_setReturn
(
RESULT_ERROR
,
'ERROR: The text '
.
$aParams
[
"contains"
]
.
' was NOT found in response of '
.
$aParams
[
"url"
]
.
'.'
);
}
}
return
$res
;
}
/**
* check mysql connection to a database
* @param array $aParams
* array(
* "server"
* "user"
* "password"
* "db"
* )
*/
private
function
checkMysqlConnect
(
$aParams
)
{
$this
->
_checkArrayKeys
(
$aParams
,
"server,user,password,db"
);
$db
=
mysqli_connect
(
$aParams
[
"server"
],
$aParams
[
"user"
],
$aParams
[
"password"
],
$aParams
[
"db"
]
);
if
(
$db
)
{
$this
->
_setReturn
(
RESULT_OK
,
"OK: Mysql database "
.
$aParams
[
"db"
]
.
" was connected"
);
return
true
;
}
else
{
$this
->
_setReturn
(
RESULT_ERROR
,
"ERROR: Mysql database "
.
$aParams
[
"db"
]
.
" was not connected. "
.
mysqli_connect_error
());
return
false
;
}
}
/**
* check sqlite connection
* @param array $aParams
* array(
* "db"
* )
* @return boolean
*/
private
function
checkSqliteConnect
(
$aParams
)
{
$this
->
_checkArrayKeys
(
$aParams
,
"db"
);
if
(
!
file_exists
(
$aParams
[
"db"
]))
{
$this
->
_setReturn
(
RESULT_ERROR
,
"ERROR: Sqlite database file "
.
$aParams
[
"db"
]
.
" does not exist."
);
return
false
;
}
try
{
// $db = new SQLite3($sqliteDB);
// $db = new PDO("sqlite:".$sqliteDB);
$o
=
new
PDO
(
"sqlite:"
.
$aParams
[
"db"
]);
$this
->
_setReturn
(
RESULT_OK
,
"OK: Sqlite database "
.
$aParams
[
"db"
]
.
" was connected"
);
return
true
;
}
catch
(
Exception
$exc
)
{
$this
->
_setReturn
(
RESULT_ERROR
,
"ERROR: Sqlite database "
.
$aParams
[
"db"
]
.
" was not connected. "
.
mysqli_connect_error
());
return
false
;
}
}
/**
* check if system is listening to a given port
* @param array $aParams
* array(
* "port"
* "host" (optional: 127.0.0.1 is default)
* )
* @return boolean
*/
private
function
checkPortTcp
(
$aParams
)
{
$this
->
_checkArrayKeys
(
$aParams
,
"port"
);
$sHost
=
array_key_exists
(
'host'
,
$aParams
)
?
$aParams
[
'host'
]
:
'127.0.0.1'
;
$iPort
=
(
int
)
$aParams
[
'port'
];
// from http://php.net/manual/de/sockets.examples.php
$socket
=
socket_create
(
AF_INET
,
SOCK_STREAM
,
SOL_TCP
);
if
(
$socket
===
false
)
{
$this
->
_setReturn
(
RESULT_UNKNOWN
,
"ERROR:
$sHost
:
$iPort
was not checked. socket_create() failed: "
.
socket_strerror
(
socket_last_error
()));
return
false
;
}
$result
=
socket_connect
(
$socket
,
$sHost
,
$iPort
);
if
(
$result
===
false
)
{
$this
->
_setReturn
(
RESULT_ERROR
,
"ERROR:
$sHost
:
$iPort
failed. "
.
socket_strerror
(
socket_last_error
(
$socket
)));
socket_close
(
$socket
);
return
false
;
}
else
{
$this
->
_setReturn
(
RESULT_OK
,
"OK:
$sHost
:
$iPort
was connected."
);
socket_close
(
$socket
);
return
true
;
}
}
/**
* DEPRECATED - use checkPortTcp instead
* check if system is listening to a given port
* @param array $aParams
* array(
* "port"
* )
* @return boolean
*/
private
function
checkListeningIp
(
$aParams
)
{
$this
->
_checkArrayKeys
(
$aParams
,
"port"
);
$sResult
=
exec
(
'netstat -tulen | grep ":'
.
$aParams
[
"port"
]
.
' "'
);
if
(
$sResult
)
{
$this
->
_setReturn
(
RESULT_OK
,
"OK: Port "
.
$aParams
[
"port"
]
.
" was found: "
.
$sResult
);
return
true
;
}
else
{
$this
->
_setReturn
(
RESULT_ERROR
,
"ERROR: Port "
.
$aParams
[
"port"
]
.
" is not in use."
);
return
false
;
}
}
}
This diff is collapsed.
Click to expand it.
public_html/appmonitor/appmonitor-client.class.php
deleted
100644 → 0
+
0
−
289
View file @
e1202aa0
<?php
/**
* APPMONITOR CLIENT<br>
* <br>
* THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE <br>
* LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR <br>
* OTHER PARTIES PROVIDE THE PROGRAM ?AS IS? WITHOUT WARRANTY OF ANY KIND, <br>
* EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED <br>
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE <br>
* ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. <br>
* SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY <br>
* SERVICING, REPAIR OR CORRECTION.<br>
* <br>
* --------------------------------------------------------------------------------<br>
* <br>
* --- HISTORY:<br>
* 2014-10-24 0.5 axel.hahn@iml.unibe.ch<br>
* 2014-11-21 0.6 axel.hahn@iml.unibe.ch removed meta::ts <br>
* --------------------------------------------------------------------------------<br>
* @version 0.6
* @author Axel Hahn
* @link TODO
* @license GPL
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL 3.0
* @package IML-Appmonitor
*/
class
appmonitor
{
/**
* config: default ttl for server before requesting the client check again
* value is in seconds
* @var int
*/
private
$_iDefaultTtl
=
300
;
/**
* internal counter: greatest return value of all checks
* @var type
*/
private
$_iMaxResult
=
false
;
/**
* responded metadata of a website
* @see _createDefaultMetadata()
* @var array
*/
private
$_aMeta
=
array
();
/**
* repended array of all checks
* @see addCheck()
* @var array
*/
private
$_aChecks
=
array
();
/**
* @var array
*/
private
$_aMustKeysChecks
=
array
(
"name"
,
"description"
,
"result"
,
"value"
);
/**
* constructor: init data
*/
public
function
__construct
()
{
$this
->
_createDefaultMetadata
();
}
// ----------------------------------------------------------------------
// private function
// ----------------------------------------------------------------------
/**
* create basic array values for metadata
* @return boolean
*/
private
function
_createDefaultMetadata
()
{
$this
->
_aMeta
=
array
(
"host"
=>
false
,
"website"
=>
false
,
"ttl"
=>
false
,
"result"
=>
false
);
// fill with default values
$this
->
setHost
();
$this
->
setWebsite
();
$this
->
setTTL
();
return
true
;
}
// ----------------------------------------------------------------------
// setter
// ----------------------------------------------------------------------
/**
* set a host for metadata
* @param string $s hostname
* @return bool
*/
public
function
setHost
(
$s
=
false
)
{
if
(
!
$s
)
{
$s
=
php_uname
(
"n"
);
}
return
$this
->
_aMeta
[
"host"
]
=
$s
;
}
/**
* set a vhost for metadata
* @param string $sNewHost hostname
* @return bool
*/
public
function
setWebsite
(
$s
=
false
)
{
if
(
!
$s
)
{
$s
=
$_SERVER
[
"HTTP_HOST"
];
}
return
$this
->
_aMeta
[
"website"
]
=
$s
;
}
/**
* set a ttl value for
* @param int $iTTl TTL value in sec
* @return boolean
*/
public
function
setTTL
(
$iTTl
=
false
)
{
if
(
!
$iTTl
)
{
$iTTl
=
$this
->
_iDefaultTtl
;
}
return
$this
->
_aMeta
[
"ttl"
]
=
$iTTl
;
}
/**
* set final result in meta data; if no value was given then it
* sets the biggest value of any check.
* @param integer $iResult set resultcode
* @return boolean
*/
public
function
setResult
(
$iResult
=
false
)
{
if
(
$iResult
===
false
)
{
$iResult
=
$this
->
_iMaxResult
;
// see addCheck()
}
return
$this
->
_aMeta
[
"result"
]
=
$iResult
;
}
/**
* add a check array;
* @param type $aJob
* @return type
*/
public
function
addCheck
(
$aJob
=
array
())
{
require_once
'appmonitor-checks.class.php'
;
$oCheck
=
new
appmonitorcheck
();
// print_r($aJob); die();
$aCheck
=
$oCheck
->
makecheck
(
$aJob
);
if
(
!
$this
->
_iMaxResult
||
$aCheck
[
"result"
]
>
$this
->
_iMaxResult
)
{
$this
->
_iMaxResult
=
$aCheck
[
"result"
];
}
return
$this
->
_aChecks
[]
=
$aCheck
;
}
// ----------------------------------------------------------------------
// getter
// ----------------------------------------------------------------------
/**
* list all available check functions. This is a helper class you cann call
* to get an overview overbuilt in functions. You get a flat array with
* all function names.
* @return array
*/
public
function
listChecks
()
{
require_once
'appmonitor-checks.class.php'
;
$oCheck
=
new
appmonitorcheck
();
return
$oCheck
->
listChecks
();
}
// ----------------------------------------------------------------------
// checks
// ----------------------------------------------------------------------
/**
* verify array values and abort with all found errors
*/
private
function
_checkData
()
{
$aErrors
=
array
();
if
(
!
count
(
$this
->
_aChecks
))
{
$aErrors
[]
=
"No checks have been defined."
;
}
if
(
$this
->
_aMeta
[
"result"
]
===
false
)
{
$aErrors
[]
=
"method setResult was not used to set a final result for all checks."
;
}
if
(
count
(
$aErrors
))
{
echo
"<h1>Errors detected</h1><ol><li>"
.
implode
(
"<li>"
,
$aErrors
)
.
"</ol><hr>"
;
echo
"<pre>"
.
print_r
(
$this
->
_generateOutputArray
(),
true
)
.
"</pre><hr>"
;
die
(
"ABORT"
);
}
}
// ----------------------------------------------------------------------
// output
// ----------------------------------------------------------------------
/**
* get full array for response with metadata and Checks
* @return type
*/
private
function
_generateOutputArray
()
{
return
array
(
"meta"
=>
$this
->
_aMeta
,
"checks"
=>
$this
->
_aChecks
,
);
}
/**
* output appmonitor values as JSON
* @param bool $bPretty turn on pretty print; default is false
* @param bool $bHighlight print syntax highlighted html code; $bPretty must be true to enable
*/
public
function
render
(
$bPretty
=
false
,
$bHighlight
=
false
)
{
$this
->
_checkData
();
// JSON_PRETTY_PRINT reqires PHP 5.4
if
(
!
defined
(
'JSON_PRETTY_PRINT'
))
{
$bPretty
=
false
;
}
if
(
!
$bPretty
)
{
$bHighlight
=
false
;
$sOut
=
json_encode
(
$this
->
_generateOutputArray
());
}
else
{
$sOut
=
json_encode
(
$this
->
_generateOutputArray
(),
JSON_PRETTY_PRINT
);
if
(
$bHighlight
)
{
$aMsg
=
array
(
0
=>
"OK"
,
1
=>
"UNKNOWN"
,
2
=>
"WARNING"
,
3
=>
"ERROR"
);
foreach
(
array_keys
(
$aMsg
)
as
$iCode
){
$sOut
=
preg_replace
(
'/(\"result\":\ '
.
$iCode
.
')/'
,
'$1 <span class="result'
.
$iCode
.
'"> <--- '
.
$aMsg
[
$iCode
]
.
' </span>'
,
$sOut
);
}
$sOut
=
preg_replace
(
'/:\ \"(.*)\"/U'
,
': "<span style="color:#66e;">$1</span>"'
,
$sOut
);
$sOut
=
preg_replace
(
'/:\ ([0-9]*)/'
,
': <span style="color:#3a3; font-weight: bold;">$1</span>'
,
$sOut
);
$sOut
=
preg_replace
(
'/\"(.*)\":/U'
,
'"<span style="color:#840;">$1</span>":'
,
$sOut
);
$sOut
=
preg_replace
(
'/([{\[])/'
,
'$1<blockquote>'
,
$sOut
);
$sOut
=
preg_replace
(
'/([}\]])/'
,
'</blockquote>$1'
,
$sOut
);
$sOut
=
str_replace
(
' '
,
''
,
$sOut
);
// $sOut = preg_replace('/([{}])/', '<span style="color:#a00; ">$1</span>', $sOut);
// $sOut = preg_replace('/([\[\]])/', '<span style="color:#088; ">$1</span>', $sOut);
$sOut
=
'<!DOCTYPE html><html><head>'
.
'<style>'
.
'body{background:#e0e8f8; color:#235; font-family: verdana,arial;}'
.
'blockquote{background:rgba(0,0,0,0.03); border-left: 0px solid rgba(0,0,0,0.06); margin: 0 0 0 3em; padding: 0; border-radius: 1em; border-top-left-radius: 0;}'
.
'blockquote blockquote:hover{; }'
.
'blockquote blockquote blockquote:hover{border-color: #808;}'
.
'pre{background:rgba(0,0,0,0.05); padding: 1em; border-radius: 1em;}'
.
'.result0{background:#aca; border-right: 0em solid #080;}'
.
'.result1{background:#666; border-right: 0em solid #ccc;}'
.
'.result2{background:#fc9; border-right: 0em solid #860;}'
.
'.result3{background:#800; border-right: 0em solid #f00;}'
.
'</style>'
.
'<title>'
.
__CLASS__
.
'</title>'
.
'</head><body>'
.
'<h1>'
.
__CLASS__
.
' :: debug</h1>'
.
'<pre>'
.
$sOut
.
'</pre></body></html>'
;
}
}
if
(
!
$bHighlight
)
{
header
(
'Content-type: application/json'
);
header
(
'Cache-Control: cache'
);
header
(
'max-age: '
.
$this
->
_aMeta
[
"ttl"
]);
}
echo
$sOut
;
return
$sOut
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment