2014年5月11日 星期日

Oracle DB 移除步驟

Oracle 移除步驟如下.
1. 用 oracle 帳號登入.
2. 停掉 DB Service
3. cd $ORACLE_HOME/deinstall
4. ./deinstall
上列步驟執行完後請改用 root 做下列步驟
rm -rf /etc/oraInst.loc
附上參考網址: http://www.askmaclean.com/archives/uninstall-remove-11-2-0-2-grid-infrastructure-database-in-linux.html

Linux crontab 指令格式速查

  • crontab 指令格式

  • 範例: 
    • # Clean Expired Files
      30 5 * * 0 /bin/sh /opt/online/bin/cleanExpiredFiles.sh
    • 每個星期天的早上五點三十分執行 /opt/online/bin/cleanExpiredFiles.sh 這支 shell 程式
  •  如何在 Linux 中完成設定?
    • 可將所有的 crontab 指令編輯為文字檔,如 myCrontab.cron,放在 Linux Server 之檔案目錄中,如 /tmp/myCrontab.cron
    • 以 root 登入 Linux  Server 後在命令提示字元下輸入 crontab myCrontab.cron 即可
    • 命令提示字元下輸入 crontab -l 可查詢目前系統中設定之 crontab job
    • Linux 登入時用哪個帳號,則只有該帳號才看得到剛才設定的 crontab job,即目前只有 root 這個 user 會有 crontab -l 之內容
    • root 亦可為其他 login user 建立 crontab,舉例
      • crontab -u myuser myCrontab.cron

Linux find 指令 速查

【 基本語法 】

查詢檔案名稱 ( 也可以查詢「目錄名稱」,其中 * 是萬用字元 )

find $HOME -name '*.mp3'

   註1: $HOME 是 Linux 下的一個環境變數,預設指向執行帳號的 HOME 目錄

查詢檔案名稱 (不區分大小寫)

find /etc -iname 'Network'

指定只要搜尋「檔案」名稱

find /var/log -iname '*.log' -type f

指定只要搜尋「目錄」名稱

find /etc -iname 'apache2' -type d

   補充說明:可用的 –type 參數值如下

    b      block (buffered) special

    c      character (unbuffered) special

    d      directory ( 一般目錄 )

    p      named pipe (FIFO)

    f      regular file ( 一般檔案 )

    l      symbolic link

    s      socket

    D      door (Solaris)

找尋所有檔案大小大於 50MB 的檔案

find /var -type f -size +50M

   註1: 不加上 –name 參數即代表搜尋所有檔案

找尋所有檔案大小小於 50MB 的檔案

find /var -type f -size -50M

尋找超過 7 天沒有被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -atime +7

尋找曾經在 7 天內被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -atime -7

尋找超過 10 分鐘沒有被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -amin +10

尋找曾經在 10 分鐘內被存取或修改過的檔案 (判斷檔案存取時間)

find $HOME -type f -amin -10

尋找檔案建立時間已超過 30 天的檔案

find $HOME -type f -ctime +30

尋找特定使用者的檔案 ( 以帳號名稱 tom 為例 )

find $HOME -type f -user tom

【 進階應用 】

刪除 30 天以上未經存取過的暫存檔案 ( 注意: 以下指令最後一個分號(;)前一定要加上反斜線 )

find /tmp -type f -atime +30 -print -exec rm -f '{}' \;

   註1: 加上 –print 是為了讓被刪除的檔案檔名一併顯示在畫面上,這個參數可以省略
   註2: 使用 –exec 會讓查詢到的每一個檔案路徑代入 ‘{}’ 位置,一個檔案會執行一遍 rm 命令

刪除 30 天以上未經存取過的暫存檔案 ( 使用 xargs 當成單一命令的參數 )

find /tmp -type f -print0 | xargs -0 rm -v

   註1: 加上 –print0 是為了讓輸出的結果不以「斷行字元」分隔,而改以 null 為結果的分隔字元
   註2: 使用 xargs 命令加上 –0 是為了讓傳入的資料以 null 字元當成參數的分隔
   註3: 使用 rm 命令加上 –v 是為了能顯示出被刪除的檔案名稱,這個參數可以省略
   註4: 使用 xargs 會將所有 find 命令查到的檔案轉換成 rm 的參數列,如果檔案過多可能會執行失敗!
   註5: 使用 xargs 可確保後面的程式 ( rm ) 只執行一次,所以理論上執行速度較快!

相同參數需輸入多筆並且以「或」邏輯運算時要用 –o 參數串接起來

例1:同時找兩種檔名樣式的檔案

find $HOME -name '*.mp3' -o -user '*.ogg'

例2:同時找兩個擁有者的檔案

find /usr/local -user user1 -o -user user2



【 注意事項 】

    使用萬用字元時務必加上單引號( ' )

    !!以下是錯誤示範!!

    [user1@server ~]# find $HOME -name *.txt
    find: paths must precede expression
    Usage: find [path...] [expression]

2014年5月5日 星期一

透過 HttpClient 利用 JIRA Rest API 產生/修改 JIRA Issue


首先要下載兩個 lib,分別為 HttpClient 及JSON Simple
共有下面五個 jar file

httpclient-4.3.3.jar
httpclient-cache-4.3.3.jar
httpcore-4.3.2.jar
httpmime-4.3.3.jar
json-simple-1.1.1.jar

JIRA Rest API 可參考
https://docs.atlassian.com/jira/REST/6.2.4/

範例程式如下:

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JiraRestApiUsingHttpClient {
    private String hostURL = "http://[HOST_NAME]";
    private String restApiURL = "/rest/api/2";
    private String adminLoginURL = "http://[HOST_NAME]/login.jsp?os_username=[ADMIN_NAME]&os_password=[ADMIN_PASSWORD]";

    public String getJiraUser(String userLoginId) throws Exception {
        String url = this.hostURL + this.restApiURL + "/user?username=" + userLoginId;
       
        return this.getJiraResponse(url);
    }
   
    public String getJiraIssue(String issueKey) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String returnMessage = this.getJiraResponse(url);
        if (returnMessage.indexOf("errorMessages") > 0) {
            throw new Exception(returnMessage);
        }
        return this.getJiraResponse(url);
    }
   
    public String getIssueAssignee(String issueKey) throws Exception {
        String assignee = "";
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
        if (json.get("assignee") == null) {
            assignee = "Unassigned";
        } else {
            json = (JSONObject) new JSONParser().parse(json.get("assignee").toString());
            assignee = json.get("name").toString();
        }
       
        return assignee;
    }
   
    public String setIssueAssignee(String issueKey, String assignee) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey + "/assignee";
        String jsonData = "{\"name\": \"" + assignee + "\"}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssueReporter(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
        json = (JSONObject) new JSONParser().parse(json.get("reporter").toString());
       
        return json.get("name").toString();
    }
   
    public String setIssueReporter(String issueKey, String reporter) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"reporter\": {"
                    + "\"name\": \"" + reporter + "\""
                + "}"
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssueSummary(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
       
        return json.get("summary").toString();
    }
   
    public String setIssueSummary(String issueKey, String summary) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"summary\": \"" + summary + "\""
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssueDescription(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
       
        return json.get("description") == null ? "" : json.get("description").toString();
    }
   
    public String setIssueDescription(String issueKey, String description) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"description\": \"" + description + "\""
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String[] getIssueComponents(String issueKey) throws Exception {
        String[] returnStringArray = null;
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
       
        if (json.get("components") != null) {
            JSONArray jsonArray = (JSONArray) new JSONParser().parse(json.get("components").toString());
            returnStringArray = new String[jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject arrayObj = (JSONObject) new JSONParser().parse(jsonArray.get(i).toString());
                returnStringArray[0] = arrayObj.get("name").toString();
                System.out.println(arrayObj.get("name").toString());
            }
        }
       
        return returnStringArray;
    }
   
    public String setIssueComponents(String issueKey, String components) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"components\": [{"
                    + "\"name\": \"" + components + "\""
                + "}]"
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssuePriority(String issueKey) throws Exception {
        String priority = null;
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
        if (json.get("priority") != null) {
            json = (JSONObject) new JSONParser().parse(json.get("priority").toString());
            priority = json.get("name").toString();
        }
       
        return priority;
    }
   
    public String setIssuePriority(String issueKey, String priority) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"priority\": {"
                    + "\"name\": \"" + priority + "\""
                + "}"
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssueType(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
        json = (JSONObject) new JSONParser().parse(json.get("issuetype").toString());
       
        return json.get("name").toString();
    }
   
    public String setIssueType(String issueKey, String issueType) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey;
        String jsonData = "{\"fields\": {"
                + "\"issuetype\": {"
                    + "\"name\": \"" + issueType + "\""
                + "}"
            + "}"
        + "}";
       
        return this.putJiraUpdatedData(url, jsonData);
    }
   
    public String getIssueStatus(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
        json = (JSONObject) new JSONParser().parse(json.get("status").toString());
       
        return json.get("name").toString();
    }
   
    public String getIssueResolution(String issueKey) throws Exception {
        String issue = this.getJiraIssue(issueKey);
        JSONObject json = (JSONObject) new JSONParser().parse(issue);
        json = (JSONObject) new JSONParser().parse(json.get("fields").toString());
       
        return json.get("resolution") == null ? "Unresolved" : json.get("resolution").toString();
    }
   
    public String addIssueComment(String issueKey, String comment) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/" + issueKey + "/comment";
        String jsonData = "{\"body\": \"" + comment + "\"}";
       
        return this.postJiraCreateData(url, jsonData);
    }
   
    public String createIssue(IssueInfo issueInfo) throws Exception {
        String url = this.hostURL + this.restApiURL + "/issue/";
        String jsonData = "{\"fields\": {"
                + "\"summary\": \"" + issueInfo.getSummary() + "\","
                + "\"description\": \"" + issueInfo.getSummary() + "\","
                + "\"project\": {"
                    + "\"key\": \"" + issueInfo.getJiraProject() + "\""
                + "},"
                + "\"reporter\": {"
                    + "\"name\": \"" + issueInfo.getReporter() + "\""
                + "},"
                + "\"assignee\": {"
                    + "\"name\": \"" + issueInfo.getAssignee() + "\""
                + "},"
                + "\"issuetype\": {"
                    + "\"name\": \"" + issueInfo.getIssuetype() + "\""
                + "}"
            + "}"
        + "}";
       
        return this.postJiraCreateData(url, jsonData);
    }
   
    private String getJiraResponse(String url) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username",
                "password"));
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        try {
            HttpPost httpPost = new HttpPost(this.adminLoginURL);
            HttpGet httpGet = new HttpGet(url);

            CloseableHttpResponse postResponse = httpclient.execute(httpPost);
            CloseableHttpResponse getResponse = httpclient.execute(httpGet);
            try {
                String bodyAsString = EntityUtils.toString(getResponse.getEntity());

                return bodyAsString;
            } finally {
                postResponse.close();
                getResponse.close();
            }
        } finally {
            httpclient.close();
        }
    }

    private String putJiraUpdatedData(String url, String jsonData) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username",
                "password"));
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        try {
            HttpPost httpPost = new HttpPost(this.adminLoginURL);
            HttpPut httpPut = new HttpPut(url);
            StringEntity entity = new StringEntity(jsonData);
            entity.setContentType("application/json; charset=UTF-8");
            httpPut.setEntity(entity);
           
            CloseableHttpResponse postResponse = httpclient.execute(httpPost);
            CloseableHttpResponse putResponse = httpclient.execute(httpPut);
            try {
                String bodyAsString = putResponse.getStatusLine().toString();
 
                return bodyAsString;
            } finally {
                postResponse.close();
                putResponse.close();
            }
        } finally {
            httpclient.close();
        }
    }
   
    private String postJiraCreateData(String url, String jsonData) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials("username",
                "password"));
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        try {
            HttpPost httpPost = new HttpPost(this.adminLoginURL);
            HttpPost createPost = new HttpPost(url);
            StringEntity entity = new StringEntity(jsonData);
            entity.setContentType("application/json; charset=UTF-8");
            createPost.setEntity(entity);
           
            CloseableHttpResponse postResponse = httpclient.execute(httpPost);
            CloseableHttpResponse createResponse = httpclient.execute(createPost);
           
            try {
                ResponseHandler<String> handler = new BasicResponseHandler();
                String responseBody = handler.handleResponse(createResponse);
                System.out.println(responseBody);
                return responseBody;
            } finally {
                postResponse.close();
                createResponse.close();
            }
        } finally {
            httpclient.close();
        }
    }
}