Skip to content

Instantly share code, notes, and snippets.

@cygnetix
Created May 30, 2022 03:37
Show Gist options
  • Save cygnetix/980f1312b693067d4c6c3e2a452da309 to your computer and use it in GitHub Desktop.
Save cygnetix/980f1312b693067d4c6c3e2a452da309 to your computer and use it in GitHub Desktop.
Just playing around with https://vlang.io
import net
import flag
import os
enum Nagios {
ok
warning
critical
unknown
}
fn check_connection(hostname string, port int) bool {
mut conn := net.dial_tcp('$hostname:$port') or { return false }
defer {
conn.close() or {}
}
_ := conn.peer_addr() or { return false }
return true
}
fn main() {
// Parse commandline arguments
mut fp := flag.new_flag_parser(os.args)
fp.application('check_port')
fp.version('v0.0.1')
hostname := fp.string('hostname', `n`, '', 'the hostname to connect to')
port := fp.int('port', `p`, 80, 'the port number to check (default: 80)')
help := fp.bool('help', `h`, false, 'prints help then exits')
if help || hostname == '' {
println(fp.usage())
exit(0)
}
// Perform the connectivity check
if check_connection(hostname, port) {
println('Successfully connected to $hostname on tcp/$port')
exit(int(Nagios.ok))
} else {
println('Unable to connect to $hostname on tcp/$port')
exit(int(Nagios.critical))
}
}
@cygnetix
Copy link
Author

PS C:\Users\cygnetix\code\test\v> .\check_port.exe -h
check_port v0.0.1
-----------------------------------------------
Usage: check_port [options] [ARGS]

Options:
  -n, --hostname <string>   the hostname to connect to
  -p, --port <int>          the port number to check (default: 80)
  -h, --help                prints help then exits
PS C:\Users\cygnetix\code\test\v> .\check_port.exe -n google.com -p 80
Successfully connected to google.com on tcp/80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment