From 407420c6eceff8283711f98904c97c5e72c13cc3 Mon Sep 17 00:00:00 2001 From: dongjulim Date: Tue, 8 Oct 2024 15:33:46 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=99=84=EC=84=B1=20&=20r?= =?UTF-8?q?eadme=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 132 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 106 ++++++++++------------------------- src/main.rs | 15 ++++- src/metrics/disk.rs | 25 +++++++-- src/struct_set.rs | 6 +- 6 files changed, 197 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bac2898..a01cfe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -74,6 +89,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "cc" version = "1.1.21" @@ -89,6 +110,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + [[package]] name = "chumsky" version = "0.9.3" @@ -254,6 +289,29 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "1.5.0" @@ -409,6 +467,15 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lettre" version = "0.11.9" @@ -560,6 +627,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.4" @@ -809,6 +885,7 @@ dependencies = [ name = "server-storage-checker" version = "0.1.0" dependencies = [ + "chrono", "lettre", "serde", "serde_json", @@ -1013,6 +1090,61 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 13add96..e077f69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +chrono = "0.4.38" lettre = "0.11.9" serde = {version="1.0.210", features=["derive"]} serde_json = "1.0.128" diff --git a/README.md b/README.md index 4afc5cc..a6e69d5 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,45 @@ # server-storage-checker - +서버 모니터링 보조 툴 ## Getting started -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://rnd.iarkdata.com/gitlab/dongjulim/server-storage-checker.git -git branch -M main -git push -uf origin main ``` +git clone https://rnd.iarkdata.com/gitlab/dongjulim/server-storage-checker -## Integrate with your tools - -- [ ] [Set up project integrations](https://rnd.iarkdata.com/gitlab/dongjulim/server-storage-checker/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** +cd server-storage-checker -# Editing this README +! edit config.json ! -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. +cargo run +``` -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. +## Dependence +Rust toolchain : 1.8x.x-nightly version -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. +``` +$ rustup toolchain install nightly +``` +## Dependence(cargo) +- chrono = "0.4.38" +- lettre = "0.11.9" +- serde = {version="1.0.210", features=["derive"]} +- serde_json = "1.0.128" +- ssh2 = "0.9.4" ## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. +- 추가 모니터링 대상 : + - cpu + - memory + - server up/down(예외처리 안됨) -## License -For open source projects, say how it is licensed. +- 기능 + - 한번 경고한 target 경고 메일 전송 무시.
(정상 범위 내 들어오면 다시 전송 가능하게) + - password 암호화 + - confluance / mattermost api 연동 + - user 접속 후 환경변수 선택 로직 추가 + - CLI or GUI + - 사용 lib : [tui-rs](https://github.com/fdehau/tui-rs), [ratatui](https://github.com/ratatui/ratatui) 중 택 1 -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +- 예외처리 diff --git a/src/main.rs b/src/main.rs index db3cb19..fc93482 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use chrono::Local; use core::time; use ssh::GetSshSession; use std::thread; @@ -30,13 +31,21 @@ fn main() { .unwrap(); // check and add server issue(warning) - warning_vec = metrics::disk::disk_checker(&session, warning_vec); + warning_vec = metrics::disk::disk_checker(warning_vec, &session, &moniter_target); } if !warning_vec.is_empty() { - // warning_vec의 데이터를 모두 가공해서 string화 시켜서 string에 concat 하기 + let mut content: String = String::new(); + for msg in warning_vec { + content.push_str(&format!( + "Server ip: {}\nDate: {}\n{}", + msg.target.ip, + Local::now().format("%Y/%m/%d %H:%M:%S"), + msg.msg + )); + } let _ = mail_agent .clone() - .send("Disk Warnning report".to_string(), "내용".to_string()); + .send("Disk Warnning report".to_string(), content); } thread::sleep(time::Duration::from_secs(60)); diff --git a/src/metrics/disk.rs b/src/metrics/disk.rs index 4a7e52b..1ecc9a3 100644 --- a/src/metrics/disk.rs +++ b/src/metrics/disk.rs @@ -1,6 +1,6 @@ use std::io::Read; -use crate::struct_set::WarningMsg; +use crate::struct_set::{ServerInfo, WarningMsg, WarningType}; use ssh2::Session; use std::str::FromStr; @@ -41,7 +41,12 @@ impl FromStr for DfResultInfo { } } -pub fn disk_checker(sess: &Session, warning_vec: Vec) -> Vec { +pub fn disk_checker( + mut warning_vec: Vec, + sess: &Session, + target_info: &ServerInfo, +) -> Vec { + //if target_info.os == linux let mut channel = sess.channel_session().unwrap(); channel.exec("df --total | tail -n +2").unwrap(); let mut df_result = String::new(); @@ -55,9 +60,19 @@ pub fn disk_checker(sess: &Session, warning_vec: Vec) -> Vec 60.0 { + // println!("!!!{} {}", dri.filesystem, dri.use_percentage); + let msg = WarningMsg { + war_type: WarningType::DiskFull, + target: target_info.clone(), + msg: format!( + "- [{}] mounted on [{}] has reached its capacity limit.\n - total: {} byte\n - used: {}byte ({})\n - Available: {}byte", + dri.filesystem, dri.mounted_on, dri.available + dri.used, dri.used, dri.use_percentage, dri.available + ), + }; + warning_vec.push(msg); + } } let _ = channel.wait_close(); diff --git a/src/struct_set.rs b/src/struct_set.rs index 8ba8e5d..a0f124b 100644 --- a/src/struct_set.rs +++ b/src/struct_set.rs @@ -15,9 +15,9 @@ pub enum WarningType { } pub(crate) struct WarningMsg { - war_type: WarningType, - target: ServerInfo, - msg: String, + pub(crate) war_type: WarningType, + pub(crate) target: ServerInfo, + pub(crate) msg: String, } #[derive(Serialize, Deserialize, Debug, Clone)] -- GitLab