vSphere

 View Only
  • 1.  testing a session

    Posted Apr 26, 2013 02:03 PM

    Hi,

    I would like to load a session, and test if it's still active or not.

    if so, the script use it and keep going, if not, the script connect to vcenter as usual, and save session file for re-using it next time.

    Is there an return code for $vim->load_session(session_file => '.mysession');

    here is my code:

    my $service_url = "https://$server/sdk/vimService";
    my $vim = Vim->new(service_url => $service_url);
    $vim->load_session(session_file => '.mysession');
    ######
    # here I would like to have something like
    ######
    if ($error) {
        $vim->login(user_name => $username, password => $password); 
        # save session $vim in file .mysession
        $vim->save_session(session_file => '.mysession');
    }

    But as soon as I used an inactive session, script ends with:

    Error while testing status of connection 'https://X.X.X.X/sdk/webService': The session is not authenticated.

    looked for SessionIsActive, but can't find how to use it :smileysad:
    Do you have any clue to help me please?
    Rgds


  • 2.  RE: testing a session

    Posted Apr 27, 2013 03:18 AM

    You'll need your SessionManager object -

    $session_manager = $vim->get_view(mo_ref => $vim->get_service_content()->sessionManager,
         properties => ['currentSession'] );
    $current_session = $session_manager->{currentSession};

    And finally call SessionIsActive() with the userName and key (sessionId) from the currentSession -

    $is_active = $session_manager->SessionIsActive( sessionID => $current_session->{key},

         userName => $current_session->{userName} );



  • 3.  RE: testing a session

    Posted Apr 29, 2013 08:54 AM

    Thx for your answer.


    So, basically, you have to do a connection to the server with a session, to check if the session is still ok...
    I'd would create a script to monitor host's performances, which connect to the server every 5 minutes. And to avoid these connections, I'd prefer to use sessionfiles. But if I need to have a connection to check the connection...


  • 4.  RE: testing a session

    Posted Apr 29, 2013 01:16 PM

    Wrap your load session in an eval { } block.  You should get 'The session is not authenticated' when the session cookie in the session file is no longer valid.

    eval {

                        $vim = Vim::load_session(session_file => 'sessionfile');

              };

              if ($@ =~ /The session is not authenticated/gi) {

                        # Invalid session

         # Do a full login here

              } elsif ($@) {

                        # Connection error (probably)

         # Retry later or error out since you cannot connect

              } else {

                        # Valid session.

              }



  • 5.  RE: testing a session

    Posted Apr 29, 2013 04:09 PM

    Hi again,

    so, here's my code:

    my $service_url = "https://$server/sdk/vimService";

    my $vim = Vim->new(service_url => $service_url);

    eval {

        $vim->load_session(session_file => $filesession);

    };

    if ($@ =~ /The session is not authenticated/gi) {

        # Invalid session

        print "$@\n";

        $vim->login(user_name => $username, password => $password);

         $vim->save_session(session_file => $filesession);

         print "new session\n";

    } elsif ($@) {

        # Connection error (probably)

        # Retry later or error out since you cannot connect

    } else {

        print "session ok\n";

        # Valid session.

    }

    And here's my output (less than 1 minute between the two tries):

    [server ~/vmware]$ more .mysession
    #LWP-Cookies-1.0
    Set-Cookie3: vmware_soap_session="\"52d23693-fa99-a1b0-6801-a90d8a9bd0e7\""; path="/"; domain=X.X.X.X; path_spec; discard; HttpOnly; version=0
    [server ~/vmware]$ ./test_session.pl
    Error while testing status of connection 'https://X.X.X.X/sdk/webService': The session is not authenticated.
    new session
    Connection Successful
    Session id: 520bef68-ab46-c678-652f-27454cc94a5f
    [server ~/vmware]$ more .mysession
    #LWP-Cookies-1.0
    Set-Cookie3: vmware_soap_session="\"520bef68-ab46-c678-652f-27454cc94a5f\""; path="/"; domain=X.X.X.X; path_spec; discard; HttpOnly; version=0

    Do you have any ideas why my sessionfile is not re-used?

    Thanks for your time

    Rgds,



  • 6.  RE: testing a session
    Best Answer

    Posted Apr 29, 2013 04:23 PM

    The SDK logs you out by default when the VIM global (or instance scalar) goes out of scope (it's in the deconstructor).  Try calling

    unset_logout_on_disconnect().

    $vim->unset_logout_on_disconnect();

    And make sure you aren't calling out logout().



  • 7.  RE: testing a session

    Posted Apr 30, 2013 08:55 AM

    it works :smileywink:

    thank you so much!!