4

用 Perl 把 log 丟上 Slack

 1 year ago
source link: https://blog.gslin.org/archives/2023/04/04/11127/%e7%94%a8-perl-%e6%8a%8a-log-%e4%b8%9f%e4%b8%8a-slack/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

用 Perl 把 log 丟上 Slack

因為想在 Raspberry Pi 上面把一些 log 丟上 Slack,本來看到「Stream Any Log File to Slack Using curl」這篇,但發現裡面在把字串包進 JSON object 的方法太髒 (直接把 " 換成 '),另外一方面是 Slack 已經在宣導不要用舊版的 Incoming Webhooks,所以決定用其他方式來處理。

選擇 Perl 主要是因為有一陣子沒有用 Perl 寫東西了,倒不是什麼特別的原因 (如果考慮到 Raspberry Pi 上資源有限的話,應該用 C 或是 Rust 或是 Go,但我的 Raspberry Pi 還沒忙到這種程度...),所以決定用 Perl 來實做這個程式,把 log 檔案丟上 Slack:

這邊有些比較不太常見的選擇:

  • 不用 File::Tail 而反而是用 pipe 的方式取得 tail -F 的輸出,是因為 File::Tail 非常舊了 (2015),實做上沒有用到 inotify 類的界面監控檔案的變化,反而是系統的 tail 有做。
  • JSON::PP (pure perl 版本) 而不是 JSON 是因為 JSON::PP 在 Perl 5.14+ 後內建了,我沒有太在意效能,這也是上面放 use v5.14 的原因。
  • 這邊的 API 呼叫還是選擇了 WWW::Mechanize 是因為沒有內建的套件可以處理 HTTPS 連線 (可以參考 Perl core modules 這邊),既然要另外安裝,就裝個支援度比較完整的 libwww-mechanize-perl 來用。

然後查了一下 // 的用法在 Perl 5.10+ 就支援了,好早...

Related

Perl 版本的 YUICompressor (CSS 的部份)

看到 Booking.com 把 YUICompressor 移植到 Perl 上:「Efficient CSS Compression in Perl」。模組在 CSS::Compressor 這裡,看日期其實放出來蠻久的了? 開發的起因是因為 Java 啟動速度太慢,所以用 Perl 改寫... 雖然只能處理 CSS 的部份,但因為現在 JS 的主力應該是 UglifyJS2 或是 UglifyJS,理論上是可以把 Java 版本的 YUICompressor 了放到旁邊了? 據開發者自己的說法,因為 Java 啟動的 overhead 實在太重,就算是處理很大的 CSS 檔案,也可以省 40% 的時間,如果是小一點的檔案甚至可以省一半...

December 17, 2012

In "Computer"

用 App::perlbrew + App::cpanminus 安裝自己的 Perl 環境

在 Ubuntu 上系統內建的 Perl 是 5.10.1,而且系統內建的 Perl module 有時會與自己裝的衝到,於是就找了一下要怎麼產生獨立環境的方法... 結果發現 gugod 早就把細節都包好 XD 首先是用系統的 cpan 安裝 App::perlbrew: cpan App::perlbrew 因為我不打算繼續用之前透過 App::cpanminus (cpanm) 裝的 module,所以我把 ~/perl5 全砍了再跑下一步,也就是初始化 perlbrew 環境:(如果是 csh/tcsh 使用者,記得先下 rehash 清 cache) perlbrew init 然後安裝 Perl 5.12.3: perlbrew install perl-5.12.3 如果安裝失敗的話,可以看 build log。我在 FreeBSD 上遇過 test fail 的情形,可以直接強制安裝,大家就自己看著辦 XD…

February 9, 2011

In "Computer"

Slack 與 Screenhero

看到 Screenhero 被 Slack 買下來的消息:「Screenhero joins Slack!」。目前只支援付費方案,不過在公司有在推動並且購買付費版 (Standard),而且使用人數還算多,所以可以拿出來用。 整合是在新聞稿丟出來之前就做完了,單鍵就可以同步帳號。 在 Ubuntu 桌機上的 Windows XP (在 VirtualBox 裡) 上想裝起來測試發現: 看起來得把 Mac 開起來弄一弄。 不過就同事丟出來的分享心得,看起來解決了之前有些同事反應的問題: 螢幕分享還可以遠端操控 teamviewer可以丟了 不過 TeamViewer 一般還是用在跟外部廠商客戶端的測試吧?倒是可以測試看看 Guest account 可不可以參與 Screenhero,如果可以的話也是不錯的選擇...

January 29, 2015

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on April 4, 2023Categories API, Computer, Murmuring, Network, Programming, SoftwareTags api, file, json, object, perl, project, side, slack, stream, tail

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK