template.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package templates
  15. import "yunion.io/x/onecloud/pkg/apis/monitor"
  16. type TemplateConfig struct {
  17. monitor.NotificationTemplateConfig
  18. }
  19. func NewTemplateConfig(c monitor.NotificationTemplateConfig) *TemplateConfig {
  20. return &TemplateConfig{
  21. NotificationTemplateConfig: c,
  22. }
  23. }
  24. const DefaultMarkdownTemplate = `
  25. ## {{.Title}}
  26. - 时间: {{.StartTime}}
  27. - 级别: {{.Level}}
  28. {{- if .Reason}}
  29. - 原因: {{.Reason}}
  30. {{- end}}
  31. {{ range .Matches}}
  32. - 指标: {{.Metric}}
  33. - 触发值: {{.ValueStr}}
  34. ### 触发条件:
  35. - {{ $.Description}}
  36. ### 标签
  37. > 名称: {{ GetValFromMap .Tags "name" }}
  38. > ip: {{ GetValFromMap .Tags "ip" }}
  39. > 平台: {{ GetValFromMap .Tags "brand" }}
  40. ------
  41. {{- end}}
  42. `
  43. func (c TemplateConfig) GenerateMarkdown() (string, error) {
  44. return CompileTEmplateFromMapText(DefaultMarkdownTemplate, c)
  45. }
  46. func (c TemplateConfig) GenerateEmailMarkdown() (string, error) {
  47. return CompileTemplateFromMapHtml(EmailMarkdownTemplate, c)
  48. }
  49. const EmailMarkdownTemplate = `
  50. <!DOCTYPE html>
  51. <html lang="en">
  52. <head>
  53. <meta charset="UTF-8">
  54. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  55. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  56. <title>{{.Title}}</title>
  57. </head>
  58. <style>
  59. .title {
  60. height: 40px;
  61. line-height: 40px;
  62. width: 960px;
  63. background-color: #4da1ff;
  64. color: #fff;
  65. font-size: 16px;
  66. text-align: center;
  67. margin: 0 auto;
  68. }
  69. .table {
  70. width: 960px;
  71. margin: 0 auto;
  72. padding: 10px 30px 0px 30px;
  73. font-family:'微软雅黑',Helvetica,Arial,sans-serif;
  74. font-size:14px;
  75. background-color: #fbfbfb;
  76. }
  77. .tr-title {
  78. height: 10px;
  79. border-left: 5px solid #4da1ff;
  80. margin-left: 10px;
  81. padding: 3px 8px;
  82. font-weight: bold;
  83. }
  84. .td {
  85. width: 80px;
  86. padding-left: 20px;
  87. height: 35px;
  88. font-weight: 400;
  89. }
  90. .link {
  91. text-decoration: none;
  92. color: #3591FF;
  93. }
  94. .thead-tr td {
  95. border-left: 1px solid #d7d7d7;
  96. border-top: 1px solid #d7d7d7;
  97. height: 32px;
  98. font-size: 14px;
  99. background-color: #d7d7d7;
  100. text-align: center;
  101. }
  102. .tbody-tr td {
  103. border-left: 1px solid #d7d7d7;
  104. border-top: 1px solid #d7d7d7;
  105. height: 32px;
  106. font-size: 14px;
  107. font-weight: 400;
  108. text-align: center;
  109. }
  110. .pb-3 {
  111. padding-bottom: 30px;
  112. }
  113. .resouce-table {
  114. width: 98%;
  115. color: #717171;
  116. border-right: 1px solid #d7d7d7;
  117. border-bottom: 1px solid #d7d7d7;
  118. }
  119. </style>
  120. <body>
  121. <h3 class="title">报警提醒</h3>
  122. <table border="0" cellspacing="0" cellpadding="0" class="table">
  123. <tr><td colspan="4" class="tr-title">报警信息</td></tr>
  124. <tr><td style="height: 10px;"></td></tr>
  125. <tr>
  126. <td class="td">报警策略:</td>
  127. <td>{{.Name}}</td>
  128. </tr>
  129. <tr>
  130. <td class="td">报警级别:</td>
  131. <td>{{.Level}}</td>
  132. </tr>
  133. <tr>
  134. <td class="td">报警时间:</td>
  135. <td>{{.StartTime}}</td>
  136. </tr>
  137. <tr>
  138. <td class="td">策略详情:</td>
  139. <td>{{.Description}}</td>
  140. </tr>
  141. {{ if .Reason }}
  142. <tr>
  143. <td class="td">报警原因:</td>
  144. <td>{{.Reason}}</td>
  145. </tr>
  146. {{ end }}
  147. </table>
  148. <table class="table" style="padding-top: 6px; padding-bottom: 10px;">
  149. <tr>
  150. <td style="padding-left: 20px; font-size: 14px;">若要查看详情信息,<a class="link" target="_blank" href="{{.WebUrl}}/commonalerts
  151. ">请登录平台进行查看</a></td>
  152. </tr>
  153. </table>
  154. <table border="0" cellspacing="0" cellpadding="0" class="table pb-3">
  155. <tr><td colspan="4" class="tr-title">报警资源</td></tr>
  156. <tr><td style="height: 10px;"></td></tr>
  157. <tr>
  158. <td colspan="4" style="padding: 10px 0 0 20px;">
  159. <table cellspacing="0" cellpadding="0" class="resouce-table">
  160. <thead>
  161. <tr class="thead-tr">
  162. <td>序号</td>
  163. <td>名称</td>
  164. <td>IP</td>
  165. <td>平台</td>
  166. <td>触发值</td>
  167. </tr>
  168. </thead>
  169. <tbody>
  170. {{- range $i, $Matche := .Matches}}
  171. <tr class="tbody-tr">
  172. <td>{{ Inc $i}}</td>
  173. <td>
  174. {{- GetValFromMap .Tags "name"}}
  175. </td>
  176. <td>
  177. {{ GetValFromMap .Tags "ip" }}
  178. </td>
  179. <td>
  180. {{ GetValFromMap .Tags "brand" }}
  181. </td>
  182. <td>{{$Matche.ValueStr}}</td>
  183. </tr>
  184. {{end}}
  185. </tbody>
  186. </table>
  187. </td>
  188. </tr>
  189. <tr><td style="height: 10px;"></td></tr>
  190. </table>
  191. <p style="width: 960px; height: 40px; line-height: 40px; margin: 0 auto; background-color: #4da1ff; color: #fff; font-size: 12px; text-align: center; ">本邮件由系统自动发送,请勿直接回复!</p>
  192. </body>
  193. </html>
  194. `