Menu

How to update NetInt on Server state change

2011-02-18
2014-01-15
  • Olivier Perriot

    Olivier Perriot - 2011-02-18

    Hi, we have defined a life cycle for our server and we would remove IP on the NetworkInterface named "eht0" when the have a transition between state "production" and "decommission"

    We have defined in the class Server this tansition

    MetaModel::Init_DefineTransition("production", "ev_production_to_decommission", array("target_state"=>"decommission", "actions"=>array('RemoveEth0IP'), "user_restriction"=>null));
    

    And so the function:

    public function RemoveEth0IP($sStimulusCode) {
      $sModel = $this->Get('model');
      if ( $sModel <> "vm" ) {
        $this->Set('management_ip','');
        $this->Set('default_gateway','');
        $id = $this->GetKey();
        # print "id : $id <br>";
        $sOQL = "SELECT NetworkInterface WHERE NetworkInterface.device_id = :serverid AND NetworkInterface.name = 'eth0'";
        $oNetItfSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL),array(),array('serverid' => $id));
        while($oNetItf = $oNetItfSet->Fetch()) {
          #$sMac = $oNetItf->Get('mac_address');
          #print "$sMac"; # DEBUG to check we are in the good itf
          $oNetItf->Set('ip_address','');
        }
      }
      return true;
    }
    

    With this function, the ip of the server well removed but not the IP of the networkInterface, why ?
    What do I wrong ?
    I think it is because I am not on the "current object" and so the update are not done in the database, but so, how to do it ?

    If someone have the solution thank in advance

     
  • Denis

    Denis - 2011-02-18

    The network interface is an object of its own in iTop, so if you want to update it you'll have save it as well.

    The "main" object ($this) will be saved automatically when the new state is applied, but not the interface.

    So in your RemoveEth0IP method, after the line "$oNetItf->Set('ip_address','');", insert the line:

    $oNetItf->DBUpdate();
    
     
  • Olivier Perriot

    Olivier Perriot - 2011-02-22

    Hi,

    thank you for your quick response, we have added this line after,
    but we have now a "Error page" named "iTop - Fatal Error" with theses informations:

    Fatal error, iTop cannot continue.
    Error: DBUpdate() could not be used here, please use DBUpdateTracked() instead.
    

    We have tested with the function proposed "DBUpdateTracked()" , but the last one require an argument and I don't found where to get it.
    For our protoype, we are using the version 1.0 of itop

    Thanks

     
  • Denis

    Denis - 2011-02-23

    Replace the line:

    $oNetItf->DBUpdate();
    

    By

    $oMyChange = MetaModel::NewObject("CMDBChange");
    $oMyChange->Set("date", time());
    if (UserRights::IsImpersonated())
    {
        $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
    }
    else
    {
        $sUserString = UserRights::GetUser();
    }
    $oMyChange->Set("userinfo", $sUserString);
    $iChangeId = $oMyChange->DBInsert();
    $oNetItf->DBUpdateTracked($oMyChange);
    
     
  • Olivier Perriot

    Olivier Perriot - 2011-03-01

    It works fine,  thanks a lot !!!

     
  • jitsobullet

    jitsobullet - 2014-01-15

    Hi Olivier,

    Can i ask how you defined your Stimulus to allow you to change from 'Production' to 'Decommission'.... is it Internal or user defined.

    Would you mind sharing this please?

    Kind Regards

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.