星期二, 5月 23, 2017

grive2 with openSUSELeap42.2小記

grive2 with openSUSELeap42.2小記

今天在 facebook 看到有朋友貼 grive 的相關文章, 很早之前有試過 GNOME 的 online account with google, 不過那個時候還沒有成熟, 看到朋友回說這個是 command line 工具, 就順手玩了一下.

OS: openSUSE Leap 42.2
套件:  grive2

Github 網站

目前在 openSUSE Leap 42.2 上面, grive2 是不穩定的套件, 抱著實驗的心情來嘗試

安裝相關套件
這邊偷懶的方式我使用 OneClickInstallCLI ,  作法為 OneClickInstallCLI 後面接 .ymp位置

# OneClickInstallCLI https://software.opensuse.org/ymp/home:darkhado:openSUSE/openSUSE_Leap_42.2/grive2.ymp
If you continue, the following repositories will be subscribed:
* http://download.opensuse.org/repositories/home:/darkhado:/openSUSE/openSUSE_Leap_42.2/
If you continue, the following software packages will be installed:
* grive2
Continue? y/N 輸入 y

接下來 Import Key 當然選 Trust
2017-05-23 23-30-59 的螢幕擷圖.png


執行完畢之後就安裝完成了 :)
進行相關設定
建立同步資料夾, 這邊我跟文章一樣在家目錄建立 google_drive 目錄
> mkdir   ~/google_drive

進入資料夾
> cd   ~/google_drive/

執行驗證要求, 開啟裡面的連結
> grive -a
-----------------------
Please go to this URL and get an authentication code:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fdocs.google.com%2Ffeeds%2F+https%3A%2F%2Fdocs.googleusercontent.com%2F+https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2F&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=22314510474.apps.googleusercontent.com

-----------------------
Please input the authentication code here:

會跳出驗證要求畫面, 點選 ALLOW
2017-05-23 23-41-06 的螢幕擷圖.png

會出現authentication code
2017-05-23 23-42-57 的螢幕擷圖.png
將他貼到
-----------------------
Please input the authentication code here:

接下來就會開始同步資料夾

同步完之後觀察資料夾
  • 資料夾下會有 .grive 以及.grive_state
  • 發現沒有同步的很完整, 發現如果是 google document 會被 ignored


> grive -V
config file name "./.grive"
Reading local directories
file .grive is ignored by grive
file .grive_state is ignored by grive
Reading remote server file list
file "testfile" is a google document, ignored
folder "./grive_test" is in sync
file "Portus 淺談" is a google document, ignored
file "2013-Q1 服務學習選修名單" is a google document, ignored
file "2014-Q1 - 交大資工系服務學習項目" is a google document, ignored
file "20161022 Docker and openSUSE workshop" is a google document, ignored

結論, 可能還要再觀察


Reference:


~ enjoy it

星期二, 5月 09, 2017

20170509_python_Python程式設計實務_練習小記

20170509_python_Python程式設計實務_練習小記

Github:

Client: openSUSE Leap 42.2 with vi and PyCharm

今天練習猜數字程式, 另外把 Pycharm 升級到 2017.1.2 版本
  • VCS 要 push 的時候必須要在console輸入github 帳密才能 push
    • Linux 版本有這個問題, Mac 沒有
      • 但是 linux 如果是 command line 去使用 /opt/pycharm-community-2017.1.2/bin/pycharm.sh 就可以, 目前原因待查

練習相關檔案

檔案: 5-3.py

# -*- coding: utf-8 -*-

import random

game_count = 0
all_counts = []
while True:
  game_count += 1
  guess_count = 0
  answer = random.randint(0,99)
  while True:
      guess = int(input("請猜一個數字(0-99)"))
      guess_count += 1
      if guess == answer:
          print("恭喜你,猜中了")
          print("你總共猜了" + str(guess_count) + "次")
          all_counts.append(guess_count)
          break;
      elif guess > answer:
          print("您猜的數字太大了")
      else:
          print("您猜的數字太小了")
  #這邊有發生 pycharm console輸入的 值會產生錯誤, 但是在 command line 下就沒有問題
  onemore = input("還要再玩一次嗎?(Y/N)")
  if onemore != 'Y' and onemore != 'y':
      print("歡迎下次再來玩")
      print("您的成績如下")
      print(all_counts)
      print("平均猜中次數" + str(sum(all_counts)/float(len(all_counts))))
      break;




~ enjoy it

星期六, 4月 29, 2017

20170429 openSUSE eBPF/BCC workshop 小記

openSUSE eBPF/BCC workshop 小記

Notes:
  • Kernel config 可以參考 /boot/ 下 config-開頭對應自己目前的核心, 看相關參數有沒有開
例如
# grep   BPF   /boot/config-4.4.46-11-default
CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
CONFIG_NETFILTER_XT_MATCH_BPF=m
CONFIG_NET_CLS_BPF=m
CONFIG_NET_ACT_BPF=m
CONFIG_BPF_JIT=y
CONFIG_HAVE_BPF_JIT=y
CONFIG_BPF_EVENTS=y
# CONFIG_TEST_BPF is not set

安裝相關套件
# zypper   in   python-bcc
# zypper  in  bcc-examples

相關 examples 存放地方
# pwd
/usr/share/bcc/examples

觀察 hello world
# cat   /usr/share/bcc/examples/hello_world.py  
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

# run in project examples directory with:
# sudo ./hello_world.py"
# see trace_fields.py for a longer example

from bcc import BPF

BPF(text='void kprobe__sys_clone(void *ctx) { bpf_trace_printk("Hello, World!\\n"); }').trace_print()

安裝 ( 更新 ) 目前的 kernel-default
# zypper  in  kernel-default

重新開機
# reboot
執行相關測試

# cd   /usr/share/bcc/examples/

執行 hello_world.py
# python   /usr/share/bcc/examples/hello_world.py

接下來可以嘗試在另外一個視窗 ssh 機器

就可以觀察到相關輸出

template:/usr/share/bcc/examples # python  /usr/share/bcc/examples/hello_world.py
           sshd-2314  [001] d..2   479.045754: : Hello, World!
           sshd-2864  [000] d..2   479.072225: : Hello, World!
           sshd-2864  [000] d..2   479.172032: : Hello, World!
           sshd-2864  [000] d..2   485.447949: : Hello, World!

另外一個測試

# cat  /usr/share/bcc/examples/tracing/trace_fields.py
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

# This is an example of tracing an event and printing custom fields.
# run in project examples directory with:
# sudo ./trace_fields.py"

from bcc import BPF

prog = """
int hello(void *ctx) {
 bpf_trace_printk("Hello, World!\\n");
 return 0;
}
"""
b = BPF(text=prog)
b.attach_kprobe(event="sys_clone", fn_name="hello")
print "PID MESSAGE"
b.trace_print(fmt="{1} {5}")

進行測試
# python    /usr/share/bcc/examples/tracing/trace_fields.py
PID MESSAGE
2954 Hello, World!
2473 Hello, World!
3042 Hello, World!


其他的 examples 檔案可以在 https://github.com/iovisor/bcc/tree/master/examples 找到

# cd   /usr/share/bcc/examples/tracing/

# wget   https://raw.githubusercontent.com/iovisor/bcc/master/examples/tracing/sync_timing.py

# cat   sync_timing.py
#!/usr/bin/python
#
# sync_timing.py    Trace time between syncs.
#                   For Linux, uses BCC, eBPF. Embedded C.
#
# Written as a basic example of tracing time between events.
#
# Copyright 2016 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from __future__ import print_function
from bcc import BPF

# load BPF program
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/blkdev.h>

BPF_HASH(last);

void do_trace(struct pt_regs *ctx) {
   u64 ts, *tsp, delta, key = 0;

   // attempt to read stored timestamp
   tsp = last.lookup(&key);
   if (tsp != 0) {
       delta = bpf_ktime_get_ns() - *tsp;
       if (delta < 1000000000) {
           // output if time is less than 1 second
           bpf_trace_printk("%d\\n", delta / 1000000);
       }
       last.delete(&key);
   }

   // update stored timestamp
   ts = bpf_ktime_get_ns();
   last.update(&key, &ts);
}
""")

b.attach_kprobe(event="sys_sync", fn_name="do_trace")
print("Tracing for quick sync's... Ctrl-C to end")

# format output
start = 0
while 1:
   (task, pid, cpu, flags, ts, ms) = b.trace_fields()
   if start == 0:
       start = ts
   ts = ts - start
   print("At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms))

執行 sync_timing.py ( 另外在新視窗執行 #sync;sync;sync )
# python   /usr/share/bcc/examples/tracing/sync_timing.py
Tracing for quick sync's... Ctrl-C to end
At time 0.00 s: multiple syncs detected, last 177 ms ago
At time 0.16 s: multiple syncs detected, last 161 ms ago
At time 15.44 s: multiple syncs detected, last 1 ms ago
At time 15.44 s: multiple syncs detected, last 1 ms ago
At time 15.44 s: multiple syncs detected, last 1 ms ago


執行另外一個測試
# python   /usr/share/bcc/examples/tracing/disksnoop.py
TIME(s)            T  BYTES    LAT(ms)
4578.806008000     R  8           0.06
4580.853965000     R  8           0.06
4581.622036000     W  6144        0.12


執行另外一個測試
個人覺得這個測試很好用, 可以統計較常用的 block 大小 :)

# python    /usr/share/bcc/examples/tracing/bitehist.py
Tracing... Hit Ctrl-C to end.
^C
    kbytes              : count     distribution
        0 -> 1          : 8        |****************************************|
        2 -> 3          : 1        |*****                                   |
        4 -> 7          : 0        |                                        |
        8 -> 15         : 0        |                                        |
       16 -> 31         : 1        |*****                                   |


# cat   /usr/share/bcc/examples/tracing/bitehist.py

#!/usr/bin/python
#
# bitehist.py Block I/O size histogram.
# For Linux, uses BCC, eBPF. See .c file.
#
# Written as a basic example of using a histogram to show a distribution.
#
# The default interval is 5 seconds. A Ctrl-C will print the partially
# gathered histogram then exit.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 15-Aug-2015 Brendan Gregg Created this.

from bcc import BPF
from time import sleep

# load BPF program
b = BPF(src_file = "bitehist.c")

# header
print("Tracing... Hit Ctrl-C to end.")

# trace until Ctrl-C
try:
sleep(99999999)
except KeyboardInterrupt:
print

# output
b["dist"].print_log2_hist("kbytes")


感謝今天 AL 分享

~ enjoy it