#获取当前的时间
today = Time.new
puts "当前日期:" + today.strftime("%Y-%m-%d %H:%M:%S")#采用格式化输出
#获取指定日期的时间 如 2010-08-30 20:50:01
day = Time.local(2010, 8 , 30 , 20, 50, 1)
puts "指定日期:" + day.strftime("%Y-%m-%d %H:%M:%S")
#获取指定日期的秒数(就是1970年到指定日期的秒数)
sec1 = day.to_i
#同理,获取今天的秒数
sec2 = today.to_i
#秒数求差,同时折合为天数
num = (sec1 - sec2)/(24*60*60)
puts (day.strftime("%Y-%m-%d %H:%M:%S") + "与" + today.strftime("%Y-%m-%d %H:%M:%S") + "相差" + num.to_s + "天")
在上篇: .NET 的 ELK 监控方案 里, 我们将 Logstash 的配置节点 output 这样写:
logstash 会自动从数据中找到 i 这个字段, 并结合 @timestamp 来指定这条数据到要写到哪个 index 下面去.
如果配置中没有指定 @timestamp 从哪里取值, logstash 会默认把第一次读到取这条数据时间做为 @timestamp .
但是请注意, logstash 使用的是 UTC 时间. 这个地方将会引发标题所描述的问题.
比如, 当前时间是 2018-08-17T00:29:59.618+08:00 , 转换为 UTC 时间就成了 2018-08-16T16:29:59.618Z
Logstash 把这个 UTC 时间用来确定数据该存到哪个 Index 里面, 这就导至了 本来是 8月17日的数据, 却被写到 8月16 日的索引下面去了.
如果只是用 Kibana 做为可视化工具, 问题不大.
但是我们每天大概能收集到 100G的日志, 搜索条件多样化 , 在加上对语法不熟悉, Kibana 已经不能给我们提供便捷的使用体验了.
但是对自制的查询工具而言, 又不能跨 index 查询(有重复的日志), 所以这个问题被无限放大了.
要解决这个问题, 又不能作大规模的改动, 最好的办法就是看有什么方法把 @timestamp 用中国时间来表示.
从百度上找了两篇文章, 说用 date filter 来修改时区, 但是写到 es 里的 @timestamp 依然是 UTC 时间, 估计也是行不通, 就没有继续在上面尝试.
和之前的配置比起来, 这个新配置多了一个节点: filter
用 ruby 脚本把事件的 timestamp 的本地时间赋值给 index_day (这个 index_day 并不存在于原始的日志数据中),
然后在 output 节点的 index 中使用这个 index_day. 这样一来每天8点之前的日志就会写到正确的 index 中了.
不过, 每天结束的那几分钟产生的日志有可能会被写到下一天的索引里去..
TimeZone 配置参见 Date Filter , logstash 将日志北京时间保存成utc
Filter Ruby 参见, Logstash Filter Ruby , 不要问我, 我也不会
Output elashtic Index 配置参见 Elasticsearch output plugin
Ruby Tuesday红宝石般的星期二
She would never say where she came from
他没说过她从哪来
Yesterady don't matter if it's gone
昨天没有关系 如果已经过去
While the sun is bright
Or in the darkest night
No one knows
She comes and go
当太阳发出光芒
或在漆黑的夜里
没人知道
她的踪迹
Goodbye Ruby Tuesday
Who could hang a name on you?
When you change with ev'ry new day
Still I'm gonna miss you
再见 红宝石般的星期二
谁可以给妮找个名字
当你在新的日子改变
我还会一直想念你
Don't question why she needs to be so free
不要问为什么她要如此自由
She'll tell you it's the only way to be
她会告诉你这是唯一要做的
She just can't be chained
To a life where nothing's gained
And nothing's lost at such a cost
她仅仅是不能被束缚
为了一生不求得失
她付出太多
Goodbye Ruby Tuesday
Who could hang a name on you?
When you change with ev'ry new day
Still I'm gonna miss you
再见 红宝石般的星期二
谁可以给妮找个名字
当你在新的日子改变
我还会一直想念你
"There's no time to lose"
I heard her say
“没有时间去浪费”
我听到她说
Catch your dreams before they slip away
抓住你的梦想在它溜走前
Dying all the time
Lose your dreams and you will lose your mind
Ain't life unkind?
当这一切都结束
失去你的梦想你将没有想法
这样不残酷吗?
Goodbye Ruby Tuesday
Who could hang a name on you?
When you change with ev'ry new day
Still I'm gonna miss you
再见 红宝石般的星期二
谁可以给妮找个名字
当你在新的日子改变
我还会一直想念你
Goodbye Ruby Tuesday
Who could hang a name on you?
When you change with ev'ry new day
Still I'm gonna miss you
再见 红宝石般的星期二
谁可以给妮找个名字
当你在新的日子改变
我还会一直想念你