Search code examples
kdb

check port status on a server using q scripts


I have written a small script to check port status using q. It checks for the ports and runs an email if connection is refused, which works fine. Then I used an optional timeout for a port so it would not hang there waiting. Though it comes back with the comment "timeout" if a port is timed out, it also seems to kick me out of the function and does not get into sendemail[] part.

ProcessChecks:{[port]
            V:{@[value;("hopen (`::",(string x),";5000)");enlist]} [port];
            .keh.V:V;               :
            mail_body:(string V)," at .z.T time: ",(string .z.T);
            .keh.mail:mail_body;

  $[-6h=type V;"port accepted";SendEmail2[mail_body;"connection refused port: ",(string port)]];

The name space .keh.mail does not change and SendEmail2[] does not get sent, which is just a unix mailx command.

I will get .keh.V = "timeout"

    .keh.mail = " at .z.T time whatever the time is"

5000 is 5 second wait time and its introduction causes the problem.


Solution

  • Your issue is being caused by a stray : in the third line of your script. This is causing your function to return early. Try the following:

    ProcessChecks:{[port]
      V:{@[value;("hopen (`::",(string x),";5000)");enlist]} [port];
      .keh.V:V;
      mail_body:(string V)," at .z.T time: ",(string .z.T);
      .keh.mail:mail_body;
      $[-6h=type V;"port accepted";SendEmail2[mail_body;"connection refused port: ",(string port)]];
      }