What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

How to return values in C# with exit code?

Votes:

0

I wrote a simple C# console application that goes into SQL and returns an integer value and message followed by a return-code. The integer value is a percentage ranging from 0 to 100%. 100% is green, if it drops below 80% I have a warning.

When I run the exe the code in command line it generates the right values. However when I add the custom EXE sensor in PRTG I have a constant value of 0 and the message is 78620d7b-087c-443f-bea3-3191c7adb254

Should be... 100:ok or 67:WARNING

My main method calls the a method that prints an integer and text to the console window. Then comes back with an exit code.

    class Program
    {
        static int Main(string[] args)
        {
            int ret = HitRatio.buffHitPer();
            return ret;                   
        }
    }

Method which grabs data from the SQL server and prints to the console window.

private static int CreateCommand(string queryString, string connectionString)
        {
            int ret=1;
            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();

                SqlCommand command = new SqlCommand(queryString, connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())  //PRTG is expecting a return in the format of int64:string
                {
                    if (Convert.ToInt32(reader[0]) >= 79)
                    {
                        Console.WriteLine(Convert.ToInt16(reader[0]) + ":WARNING");
                        ret = 1;
                    }
                    else if (Convert.ToInt32(reader[0]) < 79)
                    {
                        Console.WriteLine(Convert.ToInt16(reader[0]) + ":ok");
                        ret = 0;
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(String.Format("0:{0}", Convert.ToString(e)));
                ret = 2;
            }
            finally
            {
                connection.Close();
            }
            return ret;
        }

c# integer return-code sql value

Created on Jun 15, 2017 8:36:00 PM

Last change on Jun 16, 2017 6:27:34 AM by  Luciano Lingnau [Paessler]



3 Replies

Accepted Answer

Votes:

1

Hi there,

Environment.Exit(ret)

...should do the trick :)


Kind regards,
Stephan Linke, Tech Support

Created on Jun 16, 2017 1:52:50 PM by  Stephan Linke [Paessler Support]



Votes:

0

Thanks, that gives me the return code.

It was actually a credential issue on my side, but that answer helps as well.

Created on Jun 19, 2017 2:42:06 PM



Votes:

0

Hi there,

You're welcome - glad it's solved now :)


Kind regards,
Stephan Linke, Paessler Tech Support

Created on Jun 19, 2017 3:29:13 PM by  Stephan Linke [Paessler Support]

Last change on Jun 19, 2017 3:29:22 PM by  Stephan Linke [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.