示例#1
0
        /// <summary>
        /// Perform collection task specific processing.
        /// </summary>
        ///
        /// <param name="taskId">Database assigned task Id.</param>
        /// <param name="cleId">Database Id of owning Collection Engine.</param>
        /// <param name="elementId">Database Id of element being collected.</param>
        /// <param name="databaseTimestamp">Database relatvie task dispatch timestamp.</param>
        /// <param name="localTimestamp">Local task dispatch timestamp.</param>
        /// <param name="attributes">Map of attribute names to Id for attributes being collected.</param>
        /// <param name="scriptParameters">Collection script specific parameters (name/value pairs).</param>
        /// <param name="connection">Connection script results (null if this script does not
        ///     require a remote host connection).</param>
        /// <param name="tftpDispatcher">Dispatcher for TFTP transfer requests.</param>
        ///
        /// <returns>Collection results.</returns>
        ///
        public CollectionScriptResults ExecuteTask
            (long taskId, long cleId, long elementId, long databaseTimestamp, long localTimestamp,
            IDictionary <string, string> attributes, IDictionary <string, string> scriptParameters,
            IDictionary <string, object> connection, ITftpDispatcher tftpDispatcher)
        {
            m_taskId            = taskId.ToString();
            m_cleId             = cleId;
            m_elementId         = elementId;
            m_databaseTimestamp = databaseTimestamp;
            m_localTimestamp    = localTimestamp;
            m_attributes        = attributes;
            m_scriptParameters  = scriptParameters;
            m_executionTimer    = Stopwatch.StartNew();
            ResultCodes resultCode = ResultCodes.RC_SUCCESS;

            try {
                Lib.Logger.TraceEvent(TraceEventType.Start,
                                      0,
                                      "Task Id {0}: Collection script SAPClientInfoScript",
                                      m_taskId);


                if (connection == null)
                {
                    resultCode = ResultCodes.RC_NULL_CONNECTION_OBJECT;
                    Lib.Logger.TraceEvent(TraceEventType.Error,
                                          0,
                                          "Task Id {0}: Connection object passed to SAPClientInfoScript is null.",
                                          m_taskId);
                }
                else
                {
                    //Check host attributes
                    if (connection.ContainsKey(@"address"))
                    {
                        m_strApplicationServer = connection[@"address"] as String;
                    }
                    else
                    {
                        resultCode = ResultCodes.RC_SCRIPT_PARAMETER_MISSING;
                        Lib.Logger.TraceEvent(TraceEventType.Error,
                                              0,
                                              "Task Id {0}: Missing SAP Application Server connection parameter.",
                                              m_taskId);
                    }

                    //Check InstanceNumber attributes
                    if (connection.ContainsKey(@"systemNumber"))
                    {
                        m_strInstanceNumber = connection[@"systemNumber"] as String;
                        if (m_strInstanceNumber.Length != 2)
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Error,
                                                  0,
                                                  "Task Id {0}: Invalid System number <{1}>.\nSystem Number should always be two digits even if the number is 00.",
                                                  m_taskId,
                                                  m_strInstanceNumber);
                            resultCode = ResultCodes.RC_INVALID_PARAMETER_TYPE;
                        }
                    }
                    else
                    {
                        resultCode = ResultCodes.RC_SCRIPT_PARAMETER_MISSING;
                        Lib.Logger.TraceEvent(TraceEventType.Error,
                                              0,
                                              "Task Id {0}: Missing SAP system number connection parameter.",
                                              m_taskId);
                    }

                    //Check SAP Credential
                    if (connection.ContainsKey(@"sapUserName") && connection.ContainsKey(@"sapUserPassword"))
                    {
                        m_strSAPUserName     = connection[@"sapUserName"] as String;
                        m_strSAPUserPassword = connection[@"sapUserPassword"] as String;
                    }
                    else
                    {
                        resultCode = ResultCodes.RC_SCRIPT_PARAMETER_MISSING;
                        Lib.Logger.TraceEvent(TraceEventType.Error,
                                              0,
                                              "Task Id {0}: Missing SAP User name/password connection parameter",
                                              m_taskId);
                    }
                }

                if (resultCode == ResultCodes.RC_SUCCESS)
                {
                    using (SAPGuiConnection oConn = new SAPGuiConnection()) {
                        if (!String.IsNullOrEmpty(SAPConnectionString))
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Connecting to remote machine using {1}.",
                                                  m_taskId,
                                                  SAPConnectionString);
                            Stopwatch sw = Stopwatch.StartNew();
                            oConn.Connect(SAPConnectionString, this.m_strSAPUserName, this.m_strSAPUserPassword);
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Connection to {1} complete.  Elapsed time {2}.\n{3}",
                                                  m_taskId,
                                                  m_strApplicationServer,
                                                  sw.Elapsed.ToString(),
                                                  oConn.logData.ToString());
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Collecting SAP Client information from remote SAP server.",
                                                  m_taskId);
                            sw.Reset();
                            sw.Start();
                            SAPClientInfoCollector oClientInfoCollector = new SAPClientInfoCollector(ref oConn.Session);
                            oClientInfoCollector.Collect();
                            m_sapClientInfoProperties = oClientInfoCollector.ResultString;
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Collection of client information complete.  Elapsed time {1}.\n{2}",
                                                  m_taskId,
                                                  sw.Elapsed.ToString(),
                                                  oClientInfoCollector.logData.ToString());
                        }
                    }
                    BuildDataRow();
                }
            }
            catch (Exception ex)
            {
                if (resultCode == ResultCodes.RC_SUCCESS)
                {
                    Lib.Logger.TraceEvent(TraceEventType.Error,
                                          0,
                                          "Task Id {0}: Unhandled exception in SAPClientInfoScript.  Elapsed time {1}.\n{2}\nResult code changed to RC_PROCESSING_EXCEPTION.",
                                          m_taskId,
                                          m_executionTimer.Elapsed.ToString(),
                                          ex.ToString());
                    resultCode = ResultCodes.RC_PROCESSING_EXCEPTION;
                }
                else
                {
                    Lib.Logger.TraceEvent(TraceEventType.Error,
                                          0,
                                          "Task Id {0}: Unhandled exception in SAPClientInfoScript.  Elapsed time {1}.\n{2}",
                                          m_taskId,
                                          m_executionTimer.Elapsed.ToString(),
                                          ex.ToString());
                }
            }

            Lib.Logger.TraceEvent(TraceEventType.Stop,
                                  0,
                                  "Task Id {0}: Collection script SAPClientInfoScript.  Elapsed time {1}.  Result code {2}.",
                                  m_taskId,
                                  m_executionTimer.Elapsed.ToString(),
                                  resultCode.ToString());

            return(new CollectionScriptResults(resultCode, 0, null, null, null, false, m_dataRow.ToString()));
        }
示例#2
0
        /// <summary>Connect method invoked by WinCs. </summary>
        /// <param name="taskId">Database assigned task Id.</param>
        /// <param name="connectionParameterSets">List of credential sets</param>
        /// <param name="tftpDispatcher">Tftp Listener</param>
        /// <returns>Operation results.</returns>
        public ConnectionScriptResults Connect(
            long taskId,
            IDictionary <string, string>[] connectionParameterSets,
            ITftpDispatcher tftpDispatcher)
        {
            string taskIdString            = taskId.ToString();
            ConnectionScriptResults result = null;
            Stopwatch executionTimer       = Stopwatch.StartNew();

            Lib.Logger.TraceEvent(TraceEventType.Start,
                                  0,
                                  "Task Id {0}: Connection script SAPGuiConnectionScript.",
                                  taskIdString);

            if (connectionParameterSets == null)
            {
                Lib.Logger.TraceEvent(TraceEventType.Error,
                                      0,
                                      "Task Id {0}: Null credential set passed to SAPGuiConnectionScript.",
                                      taskIdString);
                result = new ConnectionScriptResults(null, ResultCodes.RC_NULL_PARAMETER_SET, 0, -1);
            }
            else
            {
                try {
                    Lib.Logger.TraceEvent(TraceEventType.Information,
                                          0,
                                          "Task Id {0}: Executing SAPGuiConnectionScript with {1} credential sets.",
                                          taskIdString,
                                          connectionParameterSets.Length.ToString());

                    //
                    // Loop to process credential sets until a successful
                    // connection is made.
                    bool bConnectSuccess = false;
                    for (int i = 0; i < connectionParameterSets.Length && !bConnectSuccess; i++)
                    {
                        Dictionary <string, object> connectionDic = new Dictionary <string, object>();
                        foreach (KeyValuePair <string, string> kvp in connectionParameterSets[i])
                        {
                            connectionDic.Add(kvp.Key, kvp.Value);
                        }
                        string strSAPUserName       = connectionParameterSets[i][@"sapUserName"] as string;
                        string strSAPUserPassword   = connectionParameterSets[i][@"sapUserPassword"] as string;
                        string strApplicationServer = connectionParameterSets[i][@"address"] as string;
                        string strInstanceNumber    = connectionParameterSets[i][@"systemNumber"] as string;
                        if (strInstanceNumber.Length != 2)
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Error,
                                                  0,
                                                  "Task Id {0}: Invalid System number <{1}>.\nSystem Number should always be two digits even if the number is 00.",
                                                  taskIdString,
                                                  strInstanceNumber);
                            break;
                        }
                        string strConnectionString = @"/H/" + strApplicationServer.Trim() + @"/S/32" + strInstanceNumber.Trim();

                        if (!string.IsNullOrEmpty(strApplicationServer) && !string.IsNullOrEmpty(strSAPUserName) && !string.IsNullOrEmpty(strSAPUserPassword))
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Processing credential set {1}:\nConnecting to remote machine using {2}, SAP user name {3}.",
                                                  taskIdString,
                                                  i.ToString(),
                                                  strConnectionString,
                                                  strSAPUserName);

                            using (SAPGuiConnection oConn = new SAPGuiConnection()) {
                                Stopwatch sw = Stopwatch.StartNew();
                                oConn.Connect(strConnectionString, strSAPUserName.Trim(), strSAPUserPassword.Trim());
                                sw.Stop();
                                bConnectSuccess = oConn.isConnected;
                                if (oConn.isConnected)
                                {
                                    Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                          0,
                                                          "Task Id {0}: Connection to {1} with user name {2} succeeded.  Elapsed time {3}.",
                                                          taskIdString,
                                                          strApplicationServer,
                                                          strSAPUserName,
                                                          sw.Elapsed.ToString());
                                    result = new ConnectionScriptResults(connectionDic, ResultCodes.RC_SUCCESS, 0, i);
                                    break;
                                }
                                else
                                {
                                    Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                          0,
                                                          "Task Id {0}: Connection to {1} failed.  Elapsed time {2}.\n{3}",
                                                          taskIdString,
                                                          strApplicationServer,
                                                          sw.Elapsed.ToString(),
                                                          oConn.logData.ToString());
                                }
                            }
                        }
                        else
                        {
                            Lib.Logger.TraceEvent(TraceEventType.Verbose,
                                                  0,
                                                  "Task Id {0}: Skipping credential set {1} which contains null credential information.",
                                                  taskIdString,
                                                  i.ToString());
                        }
                    }
                    //
                    // Connect failed after all credentials attempted.
                    if (null == result)
                    {
                        result = new ConnectionScriptResults(null, ResultCodes.RC_HOST_CONNECT_FAILED,
                                                             0, connectionParameterSets.Length);
                    }
                } catch (Exception e) {
                    Lib.LogException(taskIdString,
                                     executionTimer,
                                     "Unhandled exception in SAPGuiConnectionScript",
                                     e);

                    //
                    // This is really an unanticipated fail safe.  We're
                    // going to report that *no* credentials were tried, which
                    // actually may not be true...
                    result = new ConnectionScriptResults(null, ResultCodes.RC_PROCESSING_EXCEPTION, 0, -1);
                }
            }

            Debug.Assert(null != result);
            Lib.Logger.TraceEvent(TraceEventType.Stop,
                                  0,
                                  "Task Id {0}: Connection script SAPGuiConnectionScript.  Elapsed time {1}.  Result code {2}.",
                                  taskIdString,
                                  executionTimer.Elapsed.ToString(),
                                  result.ResultCode.ToString());
            return(result);
        }