summaryrefslogtreecommitdiffstats
path: root/plugins/c8y_log_plugin/src/main.rs
blob: b5a7799e057aa797b2e3f378523bc5c610eed233 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
mod config;
mod error;
mod logfile_request;

use anyhow::Result;
use c8y_api::http_proxy::{C8YHttpProxy, JwtAuthHttpProxy};
use c8y_api::utils::bridge::{is_c8y_bridge_up, C8Y_BRIDGE_HEALTH_TOPIC};
use c8y_smartrest::smartrest_deserializer::{SmartRestLogRequest, SmartRestRequestGeneric};
use c8y_smartrest::topic::C8yTopic;
use clap::Parser;

use mqtt_channel::{Connection, Message, StreamExt, TopicFilter};
use std::path::{Path, PathBuf};
use tedge_config::{
    ConfigRepository, ConfigSettingAccessor, LogPathSetting, MqttPortSetting, TEdgeConfig,
    DEFAULT_TEDGE_CONFIG_PATH,
};
use tedge_utils::{
    file::{create_directory_with_user_group, create_file_with_user_group},
    fs_notify::{fs_notify_stream, pin_mut, FileEvent},
};
use thin_edge_json::health::{health_check_topics, send_health_status};
use tracing::{error, info};

use crate::config::LogPluginConfig;
use crate::logfile_request::{
    handle_dynamic_log_type_update, handle_logfile_request_operation, read_log_config,
};

const DEFAULT_PLUGIN_CONFIG_FILE: &str = "c8y/c8y-log-plugin.toml";
const AFTER_HELP_TEXT: &str = r#"On start, `c8y_log_plugin` notifies the cloud tenant of the log types listed in the `CONFIG_FILE`, sending this list with a `118` on `c8y/s/us`.
`c8y_log_plugin` subscribes then to `c8y/s/ds` listening for logfile operation requests (`522`) notifying the Cumulocity tenant of their progress (messages `501`, `502` and `503`).

The thin-edge `CONFIG_DIR` is used to store:
  * c8y-log-plugin.toml - the configuration file that specifies which logs to be retrived"#;

#[derive(Debug, clap::Parser, Clone)]
#[clap(
name = clap::crate_name!(),
version = clap::crate_version!(),
about = clap::crate_description!(),
after_help = AFTER_HELP_TEXT
)]
pub struct LogfileRequestPluginOpt {
    /// Turn-on the debug log level.
    ///
    /// If off only reports ERROR, WARN, and INFO
    /// If on also reports DEBUG and TRACE
    #[clap(long)]
    pub debug: bool,

    /// Create supported operation files
    #[clap(short, long)]
    pub init: bool,

    #[clap(long = "config-dir", default_value = DEFAULT_TEDGE_CONFIG_PATH)]
    pub config_dir: PathBuf,
}

async fn create_mqtt_client(
    tedge_config: &TEdgeConfig,
) -> Result<mqtt_channel::Connection, anyhow::Error> {
    let mqtt_port = tedge_config.query(MqttPortSetting)?.into();
    let mut topics: TopicFilter = health_check_topics("c8y-log-plugin");

    topics.add_unchecked(C8yTopic::SmartRestRequest.as_str());
    // subscribing also to c8y bridge health topic to know when the bridge is up
    topics.add(C8Y_BRIDGE_HEALTH_TOPIC)?;

    let mqtt_config = mqtt_channel::Config::default()
        .with_port(mqtt_port)
        .with_subscriptions(topics);

    let mqtt_client = mqtt_channel::Connection::new(&mqtt_config).await?;
    Ok(mqtt_client)
}

pub async fn create_http_client(
    tedge_config: &TEdgeConfig,
) -> Result<JwtAuthHttpProxy, anyhow::Error> {
    let mut http_proxy = JwtAuthHttpProxy::try_new(tedge_config).await?;
    let () = http_proxy.init().await?;
    Ok(http_proxy)
}

async fn run(
    config_dir: &Path,
    config_file_name: &str,
    mqtt_client: &mut Connection,
    http_client: &mut JwtAuthHttpProxy,
) -> Result<(), anyhow::Error> {
    let mut plugin_config = LogPluginConfig::default();

    let health_check_topics = health_check_topics("c8y-log-plugin");
    let config_file_path = config_dir.join(config_file_name);
    let () = handle_dynamic_log_type_update(&plugin_config, mqtt_client).await?;

    let fs_notification_stream = fs_notify_stream(&[(
        config_dir,
        Some(config_file_name.to_string()),
        &[FileEvent::Modified, FileEvent::Deleted, FileEvent::Created],
    )])?;
    pin_mut!(fs_notification_stream);

    loop {
        tokio::select! {
                message = mqtt_client.received.next() => {
                if let Some(message) = message {
                    process_mqtt_message(message, &plugin_config, mqtt_client, http_client, &config_file_path, health_check_topics.clone()).await?;
                } else {
                    // message is None and the connection has been closed
                    return Ok(())
                }
            }
            Some(Ok((_path, mask))) = fs_notification_stream.next() => {
                match mask {
                    FileEvent::Created | FileEvent::Deleted | FileEvent::Modified => {
                        plugin_config = read_log_config(&config_file_path);
                        let () = handle_dynamic_log_type_update(&plugin_config, mqtt_client).await?;
                    }
                }
            }
        }
    }
}

pub async fn process_mqtt_message(
    message: Message,
    plugin_config: &LogPluginConfig,
    mqtt_client: &mut Connection,
    http_client: &mut JwtAuthHttpProxy,
    config_file: &Path,
    health_check_topics: TopicFilter,
) -> Result<(), anyhow::Error> {
    if is_c8y_bridge_up(&message) {
        let plugin_config = read_log_config(config_file);
        let () = handle_dynamic_log_type_update(&plugin_config, mqtt_client).await?;
    } else if health_check_topics.accept(&message) {
        send_health_status(&mut mqtt_client.published, "c8y-log-plugin").await;
    } else if let Ok(payload) = message.payload_str() {
        let result = match payload.split(',').next().unwrap_or_default() {
            "522" => {
                info!("Log request received: {payload}");
                // retrieve smartrest object from payload
                let maybe_smartrest_obj = SmartRestLogRequest::from_smartrest(payload);
                if let Ok(smartrest_obj) = maybe_smartrest_obj {
                    handle_logfile_request_operation(
                        &smartrest_obj,
                        plugin_config,
                        mqtt_client,
                        http_client,
                    )
                    .await
                } else {
                    error!("Incorrect SmartREST payload: {}", payload);
                    Ok(())
                }
            }
            _ => {
                // Ignore operation messages not meant for this plugin
                Ok(())
            }
        };

        if let Err(err) = result {
            let error_message = format!("Handling of operation: '{}' failed with {}", payload, err);
            error!("{}", error_message);
        }
    }
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let config_plugin_opt = LogfileRequestPluginOpt::parse();
    let config_dir = PathBuf::from(
        &config_plugin_opt
            .config_dir
            .to_str()
            .unwrap_or(DEFAULT_TEDGE_CONFIG_PATH),
    );

    tedge_utils::logging::initialise_tracing_subscriber(config_plugin_opt.debug);

    // Load tedge config from the provided location
    let tedge_config_location =
        tedge_config::TEdgeConfigLocation::from_custom_root(&config_plugin_opt.config_dir);
    let config_repository = tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone());
    let tedge_config = config_repository.load()?;

    let logs_dir = tedge_config.query(LogPathSetting)?;
    let logs_dir = PathBuf::from(logs_dir.to_string());

    if config_plugin_opt.init {
        let () = init(&config_plugin_opt.config_dir, &logs_dir)?;
        return Ok(());
    }

    // Create required clients
    let mut mqtt_client = create_mqtt_client(&tedge_config).await?;
    let mut http_client = create_http_client(&tedge_config).await?;

    let () = run(
        &config_dir,
        DEFAULT_PLUGIN_CONFIG_FILE,
        &mut mqtt_client,
        &mut http_client,
    )
    .await?;
    Ok(())
}

fn init(config_dir: &Path, logs_dir: &Path) -> Result<(), anyhow::Error> {
    info!("Creating supported operation files");
    let () = create_init_logs_directories_and_files(config_dir, logs_dir)?;
    Ok(())
}

/// for the log plugin to work the following directories and files are needed:
///
/// Directories:
/// - LOGS_DIR/tedge/agent
/// - CONFIG_DIR/operations/c8y
/// - CONFIG_DIR/c8y
///
/// Files:
/// - CONFIG_DIR/operations/c8y/c8y_LogfileRequest
/// - CONFIG_DIR/c8y/c8y-log-plugin.toml
fn create_init_logs_directories_and_files(
    config_dir: &Path,
    logs_dir: &Path,
) -> Result<(), anyhow::Error> {
    // creating logs_dir
    create_directory_with_user_group(
        format!("{}/tedge", logs_dir.display()),
        "tedge",
        "tedge",
        0o755,
    )?;
    create_directory_with_user_group(
        format!("{}/tedge/agent", logs_dir.display()),
        "tedge",
        &qu