-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_splunk_hostname.rs
More file actions
108 lines (98 loc) · 3.19 KB
/
Copy pathget_splunk_hostname.rs
File metadata and controls
108 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Hostname retrieval utilities for Splunk.
//!
//! This module provides functions to retrieve hostnames from Splunk configuration
//! files, with graceful fallback to the system hostname.
use std::io;
use std::path::Path;
/// Retrieve the hostname from Splunk configuration, falling back to system hostname.
///
/// This function attempts to get the hostname from Splunk's configuration files
/// (inputs.conf and server.conf) in the local directory. If that fails, it falls
/// back to the system hostname.
///
/// # Priority order:
/// 1. `inputs.conf` stanza `[default]` key `host`
/// 2. `server.conf` stanza `[general]` key `serverName`
/// 3. System hostname
///
/// # Arguments
///
/// `splunk_root` - Path to the Splunk installation root directory.
/// If empty or invalid, system hostname is returned.
///
/// # Returns
///
/// The hostname as a String.
///
/// # Examples
///
/// ```rust,no_run
/// use std::path::Path;
/// use splunklib_rust::get_splunk_hostname::get_splunk_hostname;
///
/// let hostname = get_splunk_hostname(Path::new("/opt/splunk"));
/// println!("Hostname: {}", hostname);
/// ```
pub fn get_splunk_hostname(splunk_root: &Path) -> String {
match try_get_splunk_hostname(splunk_root) {
Ok(hostname) => hostname,
Err(_) => get_os_hostname(),
}
}
/// Try to retrieve hostname from Splunk configuration files.
///
/// Returns an error if configuration files are not accessible.
/// On success, returns the hostname found in config or system hostname as fallback.
///
/// # Arguments
///
/// * `splunk_root` - Path to the Splunk installation root directory.
///
/// # Returns
///
/// `io::Result<String>` - The hostname or an I/O error.
pub fn try_get_splunk_hostname(splunk_root: &Path) -> io::Result<String> {
let local_dir = if splunk_root.as_os_str().is_empty() {
return Ok(get_os_hostname());
} else {
splunk_root.join("etc").join("system").join("local")
};
// Read both config files in one pass
let config_files = [local_dir.join("inputs.conf"), local_dir.join("server.conf")];
let mut existing_files = Vec::new();
for f in &config_files {
if f.try_exists()? {
existing_files.push(f);
}
}
if !existing_files.is_empty() {
let dict = crate::splunk_config_processor::read_configs_default(&existing_files);
// Check inputs.conf first
if let Some(stanza) = dict.get("default")
&& let Some(host) = stanza.get("host")
{
return Ok(host.clone());
}
// Check server.conf
if let Some(stanza) = dict.get("general")
&& let Some(server_name) = stanza.get("serverName")
{
return Ok(server_name.clone());
}
}
// Fallback to system hostname
Ok(get_os_hostname())
}
/// Get the system hostname using the OS hostname facility.
///
/// This function retrieves the system hostname and falls back to "localhost"
/// if the hostname cannot be determined.
///
/// # Returns
///
/// The system hostname as a String, or "localhost" if unavailable.
pub fn get_os_hostname() -> String {
hostname::get()
.map(|h| h.to_string_lossy().into_owned())
.unwrap_or_else(|_| "localhost".to_string())
}