{"id":51,"date":"2012-04-15T04:37:00","date_gmt":"2012-04-15T04:37:00","guid":{"rendered":"\/lisa\/post\/2012\/04\/15\/CMS-example-for-saving-server-setting-info-is-there-a-better-way.aspx"},"modified":"2012-04-15T04:37:00","modified_gmt":"2012-04-15T04:37:00","slug":"cms-example-for-saving-server-setting-info-is-there-a-better-way","status":"publish","type":"post","link":"https:\/\/spacefold.com\/lisa\/2012\/04\/15\/cms-example-for-saving-server-setting-info-is-there-a-better-way\/","title":{"rendered":"CMS example for saving server setting info: is there a better way?"},"content":{"rendered":"<p>A few months ago, I posted a <a title=\"blog post on using a Policy\" href=\"\/lisa\/2012\/01\/22\/Walkthrough-Policy-Condition-Starting-something-new-and-trying-to-do-it-right\/\">walkthrough on using a Policy<\/a>&nbsp;and indicated that, although I started the process using a CMS or <a title=\"MS on CMS\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb934126\/\" target=\"_blank\" rel=\"noopener\">Central Management Server<\/a>&nbsp;query, I didn&#8217;t see a good way to do exactly what I wanted all the way through with CMS, and I was troubled by that.<\/p>\n<h2>Here&#8217;s what I mean<\/h2>\n<p>Today I&#8217;m going to describe my problem, and how I currently solve it, in more detail, because it might help you perform a common task &#8212; or, you might help me figure out how to do this common task better.<\/p>\n<h2>How do you save all your servers&#8217; settings in one place?<\/h2>\n<p>As you probably know, you can use the query <span style=\"color: #0000ff;\">select <\/span><span style=\"color: #808080;\">* <\/span><span style=\"color: #0000ff;\">FROM <\/span><span style=\"color: #008000;\">sys<\/span><span style=\"color: #808080;\">.<\/span><span style=\"color: #008000;\">configurations<\/span> to get the server-global settings in SQL Server.&nbsp; As you probably realize, you can use this query, plus @@SERVERNAME, to get this information for all the servers being managed as a group by a CMS.<\/p>\n<p>What I wanted to do seemed pretty simple and useful, and probably every DBA has a way of do it: keep a history of these settings over time, for all my servers, in one table of a management database I use for stuff like this.<\/p>\n<p>Seems obvious, right?&nbsp; But I couldn&#8217;t figure out a way to do this in one step using a CMS.&nbsp; Each iteration of the query would be issued in the context of each individual server, not the context of my management server.<\/p>\n<p>To be clear, I&#8217;m sure you can do this if you create a stored procedure on each managed server and also a linked server pointing to the CMS database in which you want to store the data &#8212; but, #1, I absolutely don&#8217;t want to create those links on each of my servers and #2, I thought that the point of a CMS was to avoid having to disperse a bunch of code among all the servers you manage.<\/p>\n<h2>How I do it now, <em>faute de mieux<\/em><\/h2>\n<p>I&#8217;ve got it down to a couple of klutzy steps.<\/p>\n<h3>1.&nbsp; Script what I want to put in the management table as a set of VALUES<\/h3>\n<p>The actual query I issue against the CMS server group looks like this:<\/p>\n<p class=\"code\">SELECT&nbsp; <span style=\"color: #ff0000;\">&#8216;(&#8216;<\/span><span style=\"color: #808080;\">+<\/span><span style=\"color: #ff00ff;\">quotename<\/span><span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">getdate<\/span><span style=\"color: #808080;\">(),<\/span><span style=\"color: #ff0000;\">&#8221;&#8221;<\/span><span style=\"color: #808080;\">)+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>quotename<span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">@@ServerName<\/span><span style=\"color: #808080;\">,<\/span><span style=\"color: #ff0000;\">&#8221;&#8221;<\/span><span style=\"color: #808080;\">)+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<\/span>quotename<span style=\"color: #808080;\">(<\/span>name<span style=\"color: #808080;\">,<\/span><span style=\"color: #ff0000;\">&#8221;&#8221;<\/span><span style=\"color: #808080;\">)+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<br \/><\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast<span style=\"color: #808080;\">(<\/span>value <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">as <\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">nvarchar<\/span><span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">max<\/span><span style=\"color: #808080;\">))+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<br \/><\/span><\/span><\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;cast<span style=\"color: #808080;\">(<\/span>value_in_use <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">as <\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">nvarchar<\/span><span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">max<\/span><span style=\"color: #808080;\">))+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<br \/><\/span><\/span><\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; cast<span style=\"color: #808080;\">(<\/span>minimum <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">as <\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">nvarchar<\/span><span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">max<\/span><span style=\"color: #808080;\">))+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<br \/><\/span><\/span><\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; cast<span style=\"color: #808080;\">(<\/span>maximum <span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">as <\/span><span style=\"color: #0000ff;\"><span style=\"color: #0000ff;\">nvarchar<\/span><span style=\"color: #808080;\">(<\/span><span style=\"color: #ff00ff;\">max<\/span><span style=\"color: #808080;\">))+<\/span><span style=\"color: #ff0000;\">&#8216;,&#8217;<\/span><span style=\"color: #808080;\">+<br \/><\/span><\/span><\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;quotename<span style=\"color: #808080;\">(<\/span>[description]<span style=\"color: #808080;\">,<\/span><span style=\"color: #ff0000;\">&#8221;&#8221;<\/span><span style=\"color: #808080;\">)<\/span><span style=\"color: #808080;\">+<\/span>&#8216;), &#8216;<br \/>FROM <span style=\"color: #008000;\">sys<\/span><span style=\"color: #808080;\">.<\/span><span style=\"color: #008000;\">configurations<br \/><\/span><span style=\"color: #0000ff;\">ORDER BY<\/span> name <br \/><span style=\"color: #008000;\">&#8212; the ORDER BY just lets me view the results a little more clearly<\/span><\/p>\n<h3>2. Receive a bunch of almost-executable text back<\/h3>\n<p>When I run the above statement, I get a single column of results that looks like this (assuming you run the results to a grid, I guess it makes just as much sense to run to text in this case):<\/p>\n<p class=\"code\"><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;access check cache bucket count&#8217;,0,0,0,65536,&#8217;Default hash bucket count for the access check result security cache&#8217;), <\/span><br \/><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;access check cache quota&#8217;,0,0,0,2147483647,&#8217;Default quota for the access check result security cache&#8217;), <\/span><br \/><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;Ad Hoc Distributed Queries&#8217;,0,0,0,1,&#8217;Enable or disable Ad Hoc Distributed Queries&#8217;), <\/span><br \/><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;affinity I\/O mask&#8217;,0,0,-2147483648,2147483647,&#8217;affinity I\/O mask&#8217;), <\/span><br \/><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;affinity mask&#8217;,0,0,-2147483648,2147483647,&#8217;affinity mask&#8217;), <\/span><br \/><span style=\"font-size: xx-small;\">(&#8216;Apr 15 2012 10:02AM&#8217;,&#8217;COCO&#8217;,&#8217;affinity64 I\/O mask&#8217;,0,0,-2147483648,2147483647,&#8217;affinity64 I\/O mask&#8217;),<\/span><\/p>\n<p>In what you see above, &#8220;COCO&#8221; is the name of my server; following COCO&#8217;s values, there is another set for each of the servers in the CMS server group.<\/p>\n<h3>3. Adjust the almost-executable text to make it executable<\/h3>\n<p>I take the stuff I get from the CMS query and copy it to a new sql script query. Since each set of values ends with a comma, I remove the comma from the final row of values and replace it with a semi-colon.<\/p>\n<p>Then I preface all the sets of values with the following INSERT statement, matching the values, and appropriate to the table (somewhat de-normalized, yes, I know) that I have chosen to use for this purpose:&nbsp;<\/p>\n<p class=\"code\"><span style=\"color: blue;\">insert<\/span><span> <span style=\"color: blue;\">into<\/span> dbo<span style=\"color: gray;\">.<\/span>SQLServerValues&nbsp;<br \/><\/span><span style=\"color: blue;\"><span>&nbsp;<\/span><\/span><span style=\"color: gray;\">(<\/span><span>[date]&nbsp;<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[server]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[name]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[value]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[value_in_use]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[minimum]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[maximum]<br \/><\/span><span><span>&nbsp;<\/span><span style=\"color: gray;\">,<\/span>[description]<span style=\"color: gray;\">)&nbsp;<br \/><\/span><\/span><span style=\"color: blue;\">values<\/span><\/p>\n<p>Then, I execute the result in the context of my management database on the correct server.<\/p>\n<h2>What do you think?<\/h2>\n<p>Is there a better way? Please let me know.&nbsp; Thanks!<\/p>\n<p>And remember: <em>the object is to get good use out of the CMS.<\/em>&nbsp; It just seems strange to me that the CMS concept is missing something that I&#8217;d consider so obvious.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few months ago, I posted a walkthrough on using a Policy&nbsp;and indicated that, although I started the process using a CMS or Central Management Server&nbsp;query, I didn&#8217;t see a good way to do exactly what I wanted all the way through with CMS, and I was troubled by that. Here&#8217;s what I mean Today<a class=\"more-link\" href=\"https:\/\/spacefold.com\/lisa\/2012\/04\/15\/cms-example-for-saving-server-setting-info-is-there-a-better-way\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/lisa\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}