1 Prérequis
Nous utiliserons LiveCode Community 9. Nous aurons besoin d'un serveur de supervision Centreon avec un compte autorisé à réaliser des requêtes API Rest.
2 Création de l'interface
Notre interface sera relativement simple pour l'instant. Nous aurons une seule fenêtre appelé stack.
3 Le code de l'interface
Il faudra rajouter la bibliothèque pour la gestion des résultats JSON. Vous trouverez cette bibliothèque à cette adresse https://github.com/luxlogica/easyjson. Il suffit de copier le contenu du fichier easyjson.lc et de le coller dans le projet du programme.
Actuellement notre projet est réduit à sa plus simple expression :
- paramétrage de la connexion Api Rest
- gestion du mot de passe (il faut bien reconnaître un peu tordu avec LiveCode)
- gestion des status des hôtes et services
- lancement de la commande avec le résultat.
- paramétrage de la connexion Api Rest
- gestion du mot de passe (il faut bien reconnaître un peu tordu avec LiveCode)
- gestion des status des hôtes et services
- lancement de la commande avec le résultat.
3.1 paramétrage de la connexion Api Rest
Nous aurons besoin de l'adresse IP du serveur Centreon, le user et son mot de passe qui est habilité au Api Rest. Il y aura aussi la gestion du mot de passe de l'objet Field password.
on keyDown pKeyName
   put the hiddenText of me into temp
   put pKeyName after temp
   set the hiddenText of me to temp
   put "*" after me
   put temp
end keyDown
on backspaceKey
   set the hiddenText of me to empty
   set the text of me to empty
end backspaceKey
   put the hiddenText of me into temp
   put pKeyName after temp
   set the hiddenText of me to temp
   put "*" after me
   put temp
end keyDown
on backspaceKey
   set the hiddenText of me to empty
   set the text of me to empty
end backspaceKey
Pour la préparation de la requête, nous utiliserons l'objet button. On récupère le token d'authentification avant de lancer la commande pour obtenir le temps réel.
on mouseUp pMouseButton
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
……..
end mouseUp
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
……..
end mouseUp
Avant d'aller plus loin, vous allons ajouter deux objets Option Menu OptHosts et OptServices qui correspond au différents états des hôtes et services.
Et voici le code pour OptHosts
global tOptHosts
-- Sent when a menu item is picked from the option menu
on menuPick pItemName
   switch pItemName
     case "all"
       put "&status=all" into tOptHosts
       break
     case "down"
       put "&status=down" into tOptHosts
       break
     case "up"
       put "&status=up" into tOptHosts
       break
     case "unreachable"
       put "&status=unreachable" into tOptHosts
       break
   end switch
end menuPick
et pour OptServices
global tOptServices
-- Sent when a menu item is picked from the option menu
on menuPick pItemName
   switch pItemName
     case "all"
       put "&status=all" into tOptServices
       break
     case "warning"
       put "&status=warning" into tOptServices
       break
     case "critical"
       put "&status=critical" into tOptServices
       break
     case "ok"
       put "&status=ok" into tOptServices
       break
     case "pending"
       put "&status=pending" into tOptServices
       break
     case "unknown"
       put "&status=unknown" into tOptServices
       break
   end switch
end menuPick
global tOptHosts
-- Sent when a menu item is picked from the option menu
on menuPick pItemName
   switch pItemName
     case "all"
       put "&status=all" into tOptHosts
       break
     case "down"
       put "&status=down" into tOptHosts
       break
     case "up"
       put "&status=up" into tOptHosts
       break
     case "unreachable"
       put "&status=unreachable" into tOptHosts
       break
   end switch
end menuPick
et pour OptServices
global tOptServices
-- Sent when a menu item is picked from the option menu
on menuPick pItemName
   switch pItemName
     case "all"
       put "&status=all" into tOptServices
       break
     case "warning"
       put "&status=warning" into tOptServices
       break
     case "critical"
       put "&status=critical" into tOptServices
       break
     case "ok"
       put "&status=ok" into tOptServices
       break
     case "pending"
       put "&status=pending" into tOptServices
       break
     case "unknown"
       put "&status=unknown" into tOptServices
       break
   end switch
end menuPick
Il nous reste à rajouter le code dans l'objet button qui permettra d'afficher le temps réel.
global tOptHosts
global tOptServices
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_hosts&action=list" & tOptHosts into tUrlHost
   put URL tUrlHost into fld "TempsReelHost"
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_services&action=list" & tOptServices into tUrlService
   put URL tUrlService into fld "TempsReelService"
end mouseUp
global tOptHosts
global tOptServices
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_hosts&action=list" & tOptHosts into tUrlHost
   put URL tUrlHost into fld "TempsReelHost"
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_services&action=list" & tOptServices into tUrlService
   put URL tUrlService into fld "TempsReelService"
end mouseUp
Attention le nombre d'affichage d'objets sera limité à 30, pour augmenter cette limite, il faudra jouer avec la variable limit. Nous verrons bientôt d'autres fonctionnalités de l'API Rest Centreon. Vous pouvez télécharger la première version de mon programme.
4 Ajout d'un timer pour les requêtes
Améliorons notre programme en rajoutant un timer pour exécuter les requêtes automatiquement toutes les minutes. Nous rajouterons un objet field nommé timeLeft. le bouton start sera renommé timer
Toute la partie du code réalisant la requête API Rest va être copié dans une fonction appelé RequeteAPIRest. On ajouterons une autre fonction pour la temporisation d'une minute, cette dernière s'appellera timerCountDown
global countDownValue
global tOptHosts
global tOptServices
on timerCountDown
   subtract 1 from CountDownValue
   if countDownValue > 0 then
     put countDownValue into field "timeLeft"
     send "timerCountDown" to me in 1 sec
   else
     if the label of button "Timer" = "Stop" then
       RequeteAPIRest
       put 60 into countDownValue
       put countDownValue into field "timeLeft"
       send "timerCountDown" to me in 1 sec
     else
       put 0 into countDownValue
       put countDownValue into field "timeLeft"
     end if
   end if
end timerCountDown
on RequeteAPIRest
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_hosts&action=list" & tOptHosts into tUrlHost
   put URL tUrlHost into fld "TempsReelHost"
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_services&action=list" & tOptServices into tUrlService
   put URL tUrlService into fld "TempsReelService"
end RequeteAPIRest
global tOptHosts
global tOptServices
on timerCountDown
   subtract 1 from CountDownValue
   if countDownValue > 0 then
     put countDownValue into field "timeLeft"
     send "timerCountDown" to me in 1 sec
   else
     if the label of button "Timer" = "Stop" then
       RequeteAPIRest
       put 60 into countDownValue
       put countDownValue into field "timeLeft"
       send "timerCountDown" to me in 1 sec
     else
       put 0 into countDownValue
       put countDownValue into field "timeLeft"
     end if
   end if
end timerCountDown
on RequeteAPIRest
   -- empty fields
   put empty into fld "TempsReelHost"
   put empty into fld "TempsReelService"
   put the fld "IpCentreon" into tIpCentreon
   put the fld "User" into tUser
   put the hiddenText of fld "Password" into tPassword
   put "Accept: application/json, text/plain, text/event-stream" & cr into couchHeaders
   put "Content-Type:application/x-www-form-urlencoded" after couchHeaders
   set httpheaders to couchHeaders
   put "username=" & tUser & "&password=" & tPassword into tData
   put "http://" & tIpCentreon & "/centreon/api/index.php?action=authenticate" into tUrl
   post tData to URL tUrl
   put JSONToArray(the urlResponse) into tAuthenticate
   put tAuthenticate["authToken"] into tToken
   put "Content-Type: application/json" & cr into couchHeaders
   put "centreon-auth-token: " after couchHeaders
   put tToken after couchHeaders
   set httpheaders to couchHeaders
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_hosts&action=list" & tOptHosts into tUrlHost
   put URL tUrlHost into fld "TempsReelHost"
   put "http://" & tIpCentreon & "/centreon/api/index.php?object=centreon_realtime_services&action=list" & tOptServices into tUrlService
   put URL tUrlService into fld "TempsReelService"
end RequeteAPIRest
Modifions le code du bouton timer
global tOptHosts
global tOptServices
global countDownValue
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
   if the label of button "Timer" = "Start" then
     set the label of button "Timer" to "Stop"
     put 60 into countDownValue
     put countDownValue into field "timeLeft"
     send "timerCountDown" to me in 1 sec
     RequeteAPIRest
   else
     set the label of button "Timer" to "Start"
     put 0 into countDownValue
     put countDownValue into field "timeLeft"
   end if
end mouseUp
global tOptServices
global countDownValue
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
   if the label of button "Timer" = "Start" then
     set the label of button "Timer" to "Stop"
     put 60 into countDownValue
     put countDownValue into field "timeLeft"
     send "timerCountDown" to me in 1 sec
     RequeteAPIRest
   else
     set the label of button "Timer" to "Start"
     put 0 into countDownValue
     put countDownValue into field "timeLeft"
   end if
end mouseUp
Le programme est maintenant modifié, lors de la première requête on déclenche un timer qui exécutera une requête API Rest automatiquement toutes les minutes. Pour arrêter les requêtes automatiques, il suffira de cliquer à nouveau sur le bouton. Vous pouvez télécharger la version 1.1 de mon programme.