News for hackers. Stuff that matters.
Re:Ask HN: A simple tcp monitor ? by huhtenberg on Sunday August 02, 2009 @03:48PM (#737970)
There you go :)

  #include <string.h>
  #include <stdio.h>
  #include <unistd.h>
  #include <netdb.h>
  
  int main(int argc, char ** argv)
  {
          struct hostent * he;
          struct sockaddr_in sa = { AF_INET };
          unsigned int port;
          int foo, sk;
  
          if (argc != 3)
                  return 1; /* Syntax error */
  
          if (sscanf(argv[2], "%u%n", &port, &foo) != 1 ||
              strlen(argv[2]) != foo ||
              port > 65535)
                  return 1; /* Syntax error */
  
          he = gethostbyname(argv[1]);
          if (! he)
                  return 2; /* DNS error,OS error or syntax error */
  
          sa.sin_addr.s_addr = *(unsigned long*)he->h_addr;
          sa.sin_port = htons(port);
  
          sk = socket(AF_INET, SOCK_STREAM, 0);
          if (sk < 0)
                  return 3; /* OS error */
  
          if (connect(sk, (void*)&sa, sizeof sa) < 0)
                  return 4; /* TCP error */
  
          return 0;
  }
To build:

  gcc tcp-check.c -o tcp-check
To use:

  ./tcp-check yahoo.com 80; echo $?
0 is OK, 1 - syntax error, 2 - DNS error, 3 - OS error, 4 - TCP error. Call it from a shell script, look at the return value and, if it's non-zero, dispatch your message. Then stick the script itself into a cron schedule, and you are done.
Re:Ask HN: A simple tcp monitor ? by jacquesm on Sunday August 02, 2009 @04:50PM (#738068)
Thank you very much for the effort :)
Re:Ask HN: A simple tcp monitor ? by spudlyo on Sunday August 02, 2009 @07:54PM (#738303)
It doesn't support timeouts.
Re:Ask HN: A simple tcp monitor ? by huhtenberg on Monday August 03, 2009 @12:33AM (#738617)
Define "timeout".