Created
May 30, 2022 03:37
-
-
Save cygnetix/980f1312b693067d4c6c3e2a452da309 to your computer and use it in GitHub Desktop.
Just playing around with https://vlang.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
Author
cygnetix
commented
May 30, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment