Version: Kingston
こちらのドキュメントによると(Version: London)
ServiceNowは「HTTP ベースの Web サービスにより、多様なアプリケーション間でのやり取りが可能になります。ServiceNow は、受信 (プロバイダー) と送信 (コンシューマー) の両方の Web サービスをサポートしています。」だそうです本記事ではREST APIを試してみたいと思います
作るもの
bashでServiceNowインスタンスにHTTP Request(POST)を飛ばして
[root@cfebb9fa5330 zabbix]# cd /usr/lib/zabbix/alertscripts
[root@cfebb9fa5330 alertscripts]# vi aaa.sh
#!/bin/bash
curl "https://********.service-now.com/api/now/table/incident" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"short_description\":\"can you explain difference between rice bowl and rice ball\"}" \
--user 'admin':'********'
:wq!
[root@cfebb9fa5330 alertscripts]# chmod 755 aaa.sh
[root@cfebb9fa5330 alertscripts]# ./aaa.sh
{"result":{"parent":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2018-07-01 06:15:38","child_incidents":"0","hold_reason":"","approval_history":"","skills":"","number":"INC0010131","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://********.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2018-07-01 06:15:38","sys_domain":{"link":"https://********.service-now.com/api/now/table/domain/09ff3d105f231000b12e3572f2b4775d","value":"09ff3d105f231000b12e3572f2b4775d"},"state":"1","task_for":"","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","u_work_notes":"","cmdb_ci":"","delivery_plan":"","contract":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"!!!/!!#/!!!/","rfc":"","time_worked":"","expected_start":"","opened_at":"2018-07-01 06:15:38","business_duration":"","group_list":"","work_end":"","caller_id":"","reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"can you explain difference between rice bowl and rice ball","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"3df6a51d4f031300bac28020a310c7ee","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":"","reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}}
[root@cfebb9fa5330 alertscripts]#
Incidentを起票します
REST API Explorerを試す
cURLでPOST
Filter navigatorにrestと入力し[System Web Services] - [▼REST] - [REST API Explorer]を表示します今回はレコードを追加したいので画面の真ん中辺りの"Create a record (POST)"をクリックしますtableNameでIncidentを選択します情報を取得したいときはGET, 編集したいときはPUT, 削除したいときはDELETEなどとHTTP Methodを使い分けるようです
画面を下にスクロールしてRequest BodyでShort Descriptionを選択して適当なメッセージを入力しますそうするとJSONのBodyが表示されます
[Send]をクリックしてみます201 CreatedでHTTP Request(POST)が成功したようです
画面を少し上にスクロールするとコードのサンプルがありましたServiceNow Script, cURL, Python, Ruby, JavaScript, Perl, Powershellとあったので試しにcURLをクリックしたところコードサンプルのダイアログが表示されました
コードをコピーして実行可能な環境でbashにして
[root@cfebb9fa5330 zabbix]# cd /usr/lib/zabbix/alertscripts
[root@cfebb9fa5330 alertscripts]# vi aaa.sh
編集してペーストして(User ID, Passwordはインスタンスのアカウントの値に合わせて編集します)
#!/bin/bash
curl "https://********.service-now.com/api/now/table/incident" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type:application/json" \
--data "{\"short_description\":\"can you explain difference between rice bowl and rice ball\"}" \
--user 'admin':'********'
保存して
:wq!
パーミッションを変更して実行して
[root@cfebb9fa5330 alertscripts]# chmod 755 aaa.sh
[root@cfebb9fa5330 alertscripts]# ./aaa.sh
{"result":{"parent":"","made_sla":"true","caused_by":"","watch_list":"","upon_reject":"cancel","sys_updated_on":"2018-07-01 06:15:38","child_incidents":"0","hold_reason":"","approval_history":"","skills":"","number":"INC0010131","resolved_by":"","sys_updated_by":"admin","opened_by":{"link":"https://********.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441","value":"6816f79cc0a8016401c5a33be04be441"},"user_input":"","sys_created_on":"2018-07-01 06:15:38","sys_domain":{"link":"https://********.service-now.com/api/now/table/domain/09ff3d105f231000b12e3572f2b4775d","value":"09ff3d105f231000b12e3572f2b4775d"},"state":"1","task_for":"","sys_created_by":"admin","knowledge":"false","order":"","calendar_stc":"","closed_at":"","u_work_notes":"","cmdb_ci":"","delivery_plan":"","contract":"","impact":"3","active":"true","work_notes_list":"","business_service":"","priority":"5","sys_domain_path":"!!!/!!#/!!!/","rfc":"","time_worked":"","expected_start":"","opened_at":"2018-07-01 06:15:38","business_duration":"","group_list":"","work_end":"","caller_id":"","reopened_time":"","resolved_at":"","approval_set":"","subcategory":"","work_notes":"","short_description":"can you explain difference between rice bowl and rice ball","close_code":"","correlation_display":"","delivery_task":"","work_start":"","assignment_group":"","additional_assignee_list":"","business_stc":"","description":"","calendar_duration":"","close_notes":"","notify":"1","sys_class_name":"incident","closed_by":"","follow_up":"","parent_incident":"","sys_id":"3df6a51d4f031300bac28020a310c7ee","contact_type":"","reopened_by":"","incident_state":"1","urgency":"3","problem_id":"","company":"","reassignment_count":"0","activity_due":"","assigned_to":"","severity":"3","comments":"","approval":"not requested","sla_due":"","comments_and_work_notes":"","due_date":"","sys_mod_count":"0","reopen_count":"0","sys_tags":"","escalation":"0","upon_approval":"proceed","correlation_id":"","location":"","category":"inquiry"}}
[root@cfebb9fa5330 alertscripts]#
Incidentを起票出来ているようです
ZABBIXでalertが発生したときのスクリプトに指定してZABBIXと連携なども出来そうです
ServiceNow Script(JavaScript)で自分自身のInstanceに対して実行してみる
REST API ExplorerでPOSTでIncidentのShort Descriptionに値を追加する設定にします(cURLと手順は一緒です)
[Send]をクリックして201で成功したことを確認して
[ServiceNow Script]をクリックするとServiceNow Script Code Sampleのダイアログが表示されるので
[Select Snippet]をクリックするソースが選択されるのでCtrl + Cでコピーして
Filter navigatorにScripts - Backgroundと入力するなどしてScripts - Backgroundに移動してRun scriptのテキストボックスにCtrl + Vでペーストしてvar password = 'admin';の箇所を必要に応じて編集して
[Run script]で実行して
POST先のIncidentを確認したらレコードが追加されていました
コピペ用
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://********.service-now.com/api/now/table/incident');
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');request.setRequestBody("{\"short_description\":\"from localhost\"}");
var response = request.execute();
gs.log(response.getBody());