New Question
 
 
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free PRTG
Download >>

 

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

 

Top Tags


View all Tags


How to return values in C# with exit code?

Votes:

0

Your Vote:

Up

Down

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 by  dspedzia (50) 2 1

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



3 Replies

Accepted Answer

Votes:

1

Your Vote:

Up

Down

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

Your Vote:

Up

Down

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 by  dspedzia (50) 2 1



Votes:

0

Your Vote:

Up

Down

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]



Please log in or register to enter your reply.


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.