Network Poller in Go
This challenge asks you to implement a simple network poller in Go. A network poller periodically checks the availability of a list of network hosts (IP addresses or hostnames) and reports their status. This is a useful tool for monitoring network infrastructure and identifying potential issues.
Problem Description
You are to create a Go program that takes a list of hostnames or IP addresses as input and periodically checks their reachability using ICMP (ping). The program should report the status of each host (up or down) at regular intervals. The program should handle errors gracefully, including invalid hostnames/IPs and network timeouts.
What needs to be achieved:
- Read a list of hostnames/IP addresses from a file (one host per line).
- Periodically ping each host in the list.
- Report the status of each host (Up or Down) to the console.
- Handle errors during pinging (e.g., invalid hostname, timeout, network unreachable).
- Allow the user to configure the polling interval.
Key Requirements:
- The program must be able to resolve hostnames to IP addresses.
- The program must use ICMP (ping) to check reachability.
- The program must handle timeouts gracefully.
- The program must report errors clearly.
- The program must be configurable via command-line arguments (polling interval in seconds).
Expected Behavior:
The program should continuously run, periodically pinging the hosts in the list and printing their status to the console. The output should be formatted clearly, showing the hostname/IP address and its status (Up or Down). If an error occurs during pinging, the error message should be printed to the console. The program should exit gracefully when interrupted (e.g., by pressing Ctrl+C).
Edge Cases to Consider:
- Invalid hostnames/IP addresses in the input file.
- Network timeouts.
- Hosts that are unreachable.
- DNS resolution failures.
- Empty input file.
- Invalid polling interval (e.g., negative value).
- Program interruption (Ctrl+C).
Examples
Example 1:
Input: hosts.txt (containing: google.com\n192.168.1.1\ninvalid_hostname)
Command: ./netpoller -interval 5
Output:
google.com: Up
192.168.1.1: Down
invalid_hostname: Error - could not resolve host: invalid_hostname
Explanation: The program pings google.com and 192.168.1.1. google.com is reachable, so it reports "Up". 192.168.1.1 is unreachable, so it reports "Down". The program attempts to resolve "invalid_hostname" but fails, reporting an error. The polling interval is 5 seconds.
Example 2:
Input: hosts.txt (containing: localhost\n127.0.0.1)
Command: ./netpoller -interval 2
Output:
localhost: Up
127.0.0.1: Up
Explanation: Both localhost and 127.0.0.1 are reachable, so the program reports "Up" for both.
Example 3: (Edge Case - Empty Input File)
Input: hosts.txt (empty file)
Command: ./netpoller -interval 1
Output:
No hosts to poll.
Explanation: The program detects that the input file is empty and prints a message indicating that there are no hosts to poll.
Constraints
- Input File Size: The input file (hosts.txt) can contain up to 1000 hostnames/IP addresses.
- Hostname/IP Address Length: Each hostname/IP address in the input file should be no longer than 255 characters.
- Polling Interval: The polling interval must be a positive integer in seconds. A default value of 10 seconds should be used if the interval is not provided as a command-line argument or is invalid.
- Timeout: The ping timeout should be set to 3 seconds.
- Error Handling: The program should not panic on any errors. Errors should be logged to the console.
- Performance: The program should be reasonably efficient and avoid excessive resource consumption. Avoid blocking operations where possible.
Notes
- Consider using the
netpackage for pinging and DNS resolution. - Use goroutines to ping multiple hosts concurrently to improve performance.
- Implement a mechanism to handle program interruption gracefully (e.g., using
context.Context). - Use command-line argument parsing library like
flagorcobrato handle the polling interval. - Focus on clear and concise code with proper error handling.
- The program should be runnable from the command line.
- Assume the input file is always present in the same directory as the executable.