9 Replies Latest reply on Jun 25, 2014 11:23 PM by c.gura

    Trying to create groups (containers) using perl

    WildBuzz

      Hello !

       

      I'm trying to create groups, using perl, but I can't figure out how to send the group members definition (in a xml format).

      I only get the message : "Verb Orion.Container.CreateContainer cannot unpackage parameter 6".

       

      SDK 1.5 with the patched InformationService.pm for the Invoke method.

       

      Thanks.

       

       

      orion_sdk perl

        • Re: Trying to create groups (containers) using perl
          derhally

          This is what the xml would look like

           

          <ArrayOfMemberDefinitionInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.solarwinds.com/2008/Orion">

            <MemberDefinitionInfo>

                <Name>foo</Name>

                <Definition>swis://./Orion/Orion.Nodes/NodeID=36</Definition>

            </MemberDefinitionInfo>

            <MemberDefinitionInfo>

                <Name>foo2</Name>

                <Definition>swis://./Orion/Orion.Nodes/NodeID=38</Definition>

             </MemberDefinitionInfo>

          </ArrayOfMemberDefinitionInfo>

           

          Let me know if that works out for you.

          • Re: Trying to create groups (containers) using perl
            RichardLetts

            I'm planning on doing something like get a list of campuses and building names, then for each campus create a group, and then for each building on the campus create a group with a dynamic query of the nodes whose building matches the group name. This need arose when we had a campus power outage and knowing which buildings were without power was indicated mostly by the switches in a building that were down.

             

            Would you share your script? I've several hundred buildings and I don't feel like ask the NOC staff to do this all by hand...

             

            Thanks

              • Re: Trying to create groups (containers) using perl
                WildBuzz

                Here is a sub function that I use to create groups.  I have a main group called "1-Succursales", regrouping all our stores.  In it, I create a group named "Sxxxx" representing the store number. Then, I create a dynamic query to gather all the network devices of that store, except the router, that will be the group dependency.

                 

                It may not the best way to do this, but, it works.

                The direct SQL queries are for speeding the process a bit.

                 

                Hope you find inspiration from this.

                 

                Thanks, Martin.

                 

                #-------------------------------------------------------------

                # CreateGroups

                #-------------------------------------------------------------

                sub CreateGroups

                {

                  my $nodeIP   = shift;

                  my $nodeID   = shift;

                  my $store    = shift;

                  my $swis     = shift;

                 

                 

                  my $sth = "";

                             

                  my $succGroupId=0;

                  my $groupId=0;

                  my $addDefResult;

                  my $rv;

                 

                 

                  $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name LIKE '%Succursales%'");

                  if (! defined ($sth->err()) ) {

                    $rv = $sth->execute();

                    if ( $rv != 0 ) {

                      $succGroupId = $sth->fetchrow();

                      $log->msg(1,"CreateGroups: Found master group Succursales (ID=$succGroupId).\n");

                    }

                    $sth->finish();

                  }

                 

                 

                  if ( $succGroupId != 0 ) {

                    $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name = 'S".$store."'");

                    if (! defined ($sth->err()) ) {

                      $rv = $sth->execute();

                      if ( $rv != 0 ) {

                        $groupId = $sth->fetchrow();

                        $sth->finish();

                        $log->msg(1,"CreateGroups: Group exists S".$store."(ID=$groupId).\n");

                      }

                      else {

                        $sth->finish();

                        $log->msg(1,"CreateGroups: Creating group S".$store.".\n");

                        $groupId = $swis->Invoke("Orion.Container", "CreateContainer", ["S".$store, "Core", 60, 0, "", 'true', SOAP::Data->type('xml' => "<ArrayOfMemberDefinitionInfo smlns='http://schemas.solarwinds.com/2008/Orion'><MemberDefinitionInfo><Name>S".$store."-DQ</Name><Definition>filter:/Orion.Nodes[Contains(Caption,'".$store."') AND Contains(Caption,'N[SW][CSRW]')]</Definition></MemberDefinitionInfo></ArrayOfMemberDefinitionInfo>")]);

                        while ( $groupId =~ /.*WARN.*Support.*LONG RUNNING.*/i || $groupId =~ /.*Timeout expired.*/i ) {

                          $log->msg(1,"CreateGroups: Had a timeout problem. CreateContainer ran for too long. Retrying in 30 seconds.\n");

                          sleep(10);

                          $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name = 'S".$store."'");

                          if (! defined ($sth->err()) ) {

                            $rv = $sth->execute();

                            if ( $rv != 0 ) {

                              $groupId = $sth->fetchrow();

                              $sth->finish();

                              $log->msg(1,"CreateGroups: Group has been created S".$store."(ID=$groupId).\n");

                            }

                            else {

                              $sth->finish();

                              $groupId = $swis->Invoke("Orion.Container", "CreateContainer", ["S".$store, "Core", 60, 0, "", 'true', SOAP::Data->type('xml' => "<ArrayOfMemberDefinitionInfo smlns='http://schemas.solarwinds.com/2008/Orion'><MemberDefinitionInfo><Name>S".$store."-DQ</Name><Definition>filter:/Orion.Nodes[Contains(Caption,'".$store."') AND Contains(Caption,'N[SW][CSRW]')]</Definition></MemberDefinitionInfo></ArrayOfMemberDefinitionInfo>")]);

                            }

                          }

                        }

                        if ( $groupId =~ /.*\<Return.*\>\d+\<\/Return\>.*/ ) {

                          $groupId =~ s/.*\<Return.*\>(\d+)\<\/Return\>.*/$1/;

                        }

                        $log->msg(1,"CreateGroups: Group S".$store." is created (ID=$groupId).\n");

                      }

                      if ( $groupId && $groupId != 0 ) {

                        $sth = $dbh_orion->prepare("SELECT ContainerMemberDefinitionID FROM  ContainerMemberDefinitions WHERE Name = 'S".$store."-DQ'");

                        if (! defined ($sth->err()) ) {

                          $rv = $sth->execute();

                          if ( $rv != 0 ) {

                            $addDefResult = $sth->fetchrow();

                            $sth->finish();

                            $log->msg(1,"CreateGroups: Member definition for S".$store." already exists.(ID=$addDefResult).\n");

                          }

                          else {

                            $sth->finish();

                            $addDefResult = $swis->Invoke("Orion.Container", "AddDefinition", [ $groupId, SOAP::Data->type('xml' => "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'><Name>S".$store."-DQ</Name><Definition>filter:/Orion.Nodes[Contains(Caption,'".$store."') AND Contains(Caption,'N[SW][CSRW]')]</Definition></MemberDefinitionInfo>")]);

                            while ( $addDefResult =~ /.*WARN.*Support.*LONG RUNNING.*/i || $addDefResult =~ /.*Timeout expired.*/i ) {

                              $log->msg(1,"CreateGroups: Had a timeout problem. AddDefinition ran for too long. Retrying in 30 seconds.\n");

                              sleep(10);

                              $sth = $dbh_orion->prepare("SELECT ContainerMemberDefinitionID FROM  ContainerMemberDefinitions WHERE Name = 'S".$store."-DQ'");

                              if (! defined ($sth->err()) ) {

                                $rv = $sth->execute();

                                if ( $rv != 0 ) {

                                  $addDefResult = $sth->fetchrow();

                                  $sth->finish();

                                  $log->msg(1,"CreateGroups: Member definition for S".$store." has been added.(ID=$addDefResult).\n");

                                }

                                else {

                                  $sth->finish();

                                  $addDefResult = $swis->Invoke("Orion.Container", "AddDefinition", [ $groupId, SOAP::Data->type('xml' => "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'><Name>S".$store."-DQ</Name><Definition>filter:/Orion.Nodes[Contains(Caption,'".$store."') AND Contains(Caption,'N[SW][CSRW]')]</Definition></MemberDefinitionInfo>")]);

                                }

                              }

                            }

                            if ( $addDefResult =~ /.*\<Return.*true.*\>.*/ || $addDefResult =~ /\d+/ ) {

                              $log->msg(1,"CreateGroups: Member definition added for group S".$store."(ID=$groupId).\n");

                            }

                            else {

                              $log->msg(1,"CreateGroups: ERROR: Add Def result = $addDefResult\n");

                            }

                          }

                        }

                 

                 

                        $sth = $dbh_orion->prepare("SELECT ContainerMemberDefinitionID FROM  ContainerMemberDefinitions WHERE ContainerID = $succGroupId AND Expression = 'Groups.ContainerID=".$groupId."'");

                        if (! defined ($sth->err()) ) {

                          $rv = $sth->execute();

                          if ( $rv != 0 ) {

                            $addDefResult = $sth->fetchrow();

                            $sth->finish();

                            $log->msg(1,"CreateGroups: Member definition for master Succursales group and S".$store." already exists.(ID=$addDefResult).\n");

                          }

                          else {

                            $sth->finish();

                            $addDefResult = $swis->Invoke("Orion.Container", "AddDefinition", [ $succGroupId, SOAP::Data->type('xml' => "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'><Name></Name><Definition>swis://NPMserver/Orion/Orion.Groups/ContainerID=".$groupId."</Definition></MemberDefinitionInfo>")]);

                            while ( $addDefResult =~ /.*WARN.*Support.*LONG RUNNING.*/i || $addDefResult =~ /.*Timeout expired.*/i ) {

                              $log->msg(1,"CreateGroups: Had a timeout problem. AddDefinition ran for too long. Retrying in 30 seconds.\n");

                              sleep(10);

                              $sth = $dbh_orion->prepare("SELECT ContainerMemberDefinitionID FROM ContainerMemberDefinitions WHERE ContainerID = $succGroupId AND Expression = 'Groups.ContainerID=".$groupId."'");

                              if (! defined ($sth->err()) ) {

                                $rv = $sth->execute();

                                if ( $rv != 0 ) {

                                  $addDefResult = $sth->fetchrow();

                                  $sth->finish();

                                  $log->msg(1,"CreateGroups: Member definition for master Succursales group and S".$store." has been added.(ID=$addDefResult).\n");

                                }

                                else {

                                  $sth->finish();

                                  $addDefResult = $swis->Invoke("Orion.Container", "AddDefinition", [ $succGroupId, SOAP::Data->type('xml' => "<MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'><Name></Name><Definition>swis://NPMserver/Orion/Orion.Groups/ContainerID=".$groupId."</Definition></MemberDefinitionInfo>")]);

                                }

                              }

                            }

                            if ( $addDefResult =~ /.*\<Return.*true.*\>.*/ || $addDefResult =~ /\d+/ ) {

                              $log->msg(1,"CreateGroups: Member definition added of group S".$store."(ID=$groupId) to Master group Succursales (ID=$succGroupId).\n");

                            }

                            else {

                              $log->msg(1,"CreateGroups: ERROR: Succ Group Add Def result = $addDefResult\n");

                            }

                          }

                        }

                      }

                    }

                    if ( $adsl ) {

                      CreateADSLGroup($store, $swis);

                    }

                    else {

                      $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name = 'S".$store." -  ADSL'");

                      if (! defined ($sth->err()) ) {

                        $rv = $sth->execute();

                        if ( $rv != 0 ) {

                          $groupId = $sth->fetchrow();

                          $log->msg(1,"CreateGroups: Removing group S".$store." -  ADSL.(ID=$groupId)\n");

                          my $result = $swis->Invoke("Orion.Container", "DeleteContainer", [$groupId]);

                        }

                        $sth->finish();

                      }

                    }

                 

                 

                    if ( $Internet ) {

                      CreateInternetGroup($store, $swis);

                    }

                    else {

                      $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name = 'S".$store." - Internet'");

                      if (! defined ($sth->err()) ) {

                        $rv = $sth->execute();

                        if ( $rv != 0 ) {

                          $groupId = $sth->fetchrow();

                          $log->msg(1,"CreateGroups: Removing group S".$store." - Internet.(ID=$groupId)\n");

                          my $result = $swis->Invoke("Orion.Container", "DeleteContainer", [$groupId]);

                        }

                        $sth->finish();

                      }

                    }

                 

                 

                    if ( $hspa ) {

                      CreateHSPAGroup($store, $swis);

                    }

                    else {

                      $sth = $dbh_orion->prepare("SELECT ContainerID FROM Containers WHERE Name = 'S".$store." -  HSPA'");

                      if (! defined ($sth->err()) ) {

                        $rv = $sth->execute();

                        if ( $rv != 0 ) {

                          $groupId = $sth->fetchrow();

                          $log->msg(1,"CreateGroups: Removing group S".$store." -  HSPA.(ID=$groupId)\n");

                          my $result = $swis->Invoke("Orion.Container", "DeleteContainer", [$groupId]);

                        }

                        $sth->finish();

                      }

                    }

                  }

                  else {

                    $log->msg(1,"CreateGroups: MAJOR ERROR - Succursales group not found.\n");

                    die;

                  }

                }