{"id":10668,"date":"2021-10-19T06:28:49","date_gmt":"2021-10-18T20:28:49","guid":{"rendered":"https:\/\/nsrd.info\/blog\/?p=10668"},"modified":"2021-10-19T06:28:53","modified_gmt":"2021-10-18T20:28:53","slug":"networker-basics-policy-tree-and-status","status":"publish","type":"post","link":"https:\/\/nsrd.info\/blog\/2021\/10\/19\/networker-basics-policy-tree-and-status\/","title":{"rendered":"NetWorker Basics \u2013 Policy Tree and Status"},"content":{"rendered":"\n<p>The nsrpolicy tool is remarkably powerful, allowing you to create edit and view policy details rapidly from the command line on your NetWorker server. In this post I&#8217;ll show you a simple Perl script that you can use to automatically display a &#8220;configuration tree&#8221; of your policies, and in doing so also show any currently running workflow.<\/p>\n\n\n\n<p>The flow of the script is as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Use &#8220;nsrpolicy policy list&#8221; to generate a list of the configured policies.<\/li><li>For each policy returned, display the policy name and:<ol><li>Run &#8220;nsrpolicy workflow list -p policyName&#8221; to get a list of all the workflows within the policy.<\/li><li>Run &#8220;nsrpolicy workflow display -p policyName -w workflowName&#8221; to get some basic configuration details for the workflow (enabled, auto start and start time).<\/li><li>Run &#8220;nsrpolicy monitor -p policyName -w workflowName&#8221; to find out whether the workflow is running.<\/li><li>Print the workflow and the gathered details.<\/li><li>Run &#8220;nsrpolicy action list -p policyName -w workflowName&#8221; to grab and print a list of the actions within the workflow.<\/li><\/ol><\/li><\/ol>\n\n\n\n<p>Running the script in my lab, I get the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Sun Oct 17 14:04:37]\n\n## \/nsr\/scripts  \n## root@orilla \n\n$ <strong>.\/policytree.pl <\/strong>\nAdhoc\n  -&gt; CritRebuild \n     Enabled [Y] Auto [N] Time [21:00]\n     - backup\n  -&gt; LegacyCent \n     Enabled [N] Auto [N] Time [21:00]\n     - backup\n  -&gt; LegacyTest \n     Enabled [Y] Auto [N] Time [21:00]\n     - backup\nInfrastructure\n  -&gt; Servers \n     Enabled [Y] Auto [Y] Time [23:00]\n     - backup\n  -&gt; Virtual (Active)\n     Enabled [Y] Auto [Y] Time [21:00]\n     - backup\n     - clone\nNAS\n  -&gt; DJW \n     Enabled [Y] Auto [Y] Time [15:00]\n     - backup\n  -&gt; DJW GBD Archive ()\n     Enabled [Y] Auto [N] Time [08:00]\n     - backup\n  -&gt; Finance \n     Enabled [Y] Auto [Y] Time [21:00]\n     - backup\n  -&gt; Homeshare \n     Enabled [Y] Auto [Y] Time [20:45]\n     - backup\n  -&gt; PMdG \n     Enabled [Y] Auto [Y] Time [23:30]\n     - backup\nServer Protection\n  -&gt; NMC server backup \n     Enabled [Y] Auto [Y] Time [14:00]\n     - NMC server backup\n  -&gt; Server backup \n     Enabled [Y] Auto [Y] Time [10:00]\n     - Server db backup\n     - Expiration<\/pre>\n\n\n\n<p>The script I wrote for this looks like the following:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">#!\/usr\/bin\/perl -w\n\nuse strict;\n\nmy @policies = ();\n\nif (open(NSRP,\"nsrpolicy policy list |\")) {\n\twhile (&lt;NSRP>) {\n\t\tmy $line = $_;\n\t\tchomp $line;\n\t\tpush(@policies,$line);\n\t}\n\tclose(NSRP);\n}\n\nif (@policies+0 > 0) {\n\tforeach my $policy (sort {$a cmp $b} @policies) {\n\t\tmy @workflows = ();\n\t\tprint \"$policy\\n\";\n\t\t\n\t\t# Get the workflows for this policy.\n\t\tif (open(NSRW,\"nsrpolicy workflow list -p \\'$policy\\' |\")) {\n\t\t\twhile (&lt;NSRW>) {\n\t\t\t\tmy $line = $_;\n\t\t\t\tchomp $line;\n\t\t\t\tpush(@workflows,$line);\n\t\t\t}\n\t\t\tclose(NSRW);\n\t\t\tif (@workflows+0 > 0) {\n\t\t\t\tforeach my $workflow (sort {$a cmp $b} @workflows) {\n\t\t\t\t\tmy $enabled = \"N\";\n\t\t\t\t\tmy $autostart = \"N\";\n\t\t\t\t\tmy $startTime = \" \";\n\t\t\t\t\t\n\t\t\t\t\tif (open(NSRW,\"nsrpolicy workflow display -p \\'$policy\\' -w \\'$workflow\\' |\")) {\n\t\t\t\t\t\twhile (&lt;NSRW>) {\n\t\t\t\t\t\t\tmy $line = $_;\n\t\t\t\t\t\t\tchomp $line;\n\t\t\t\t\t\t\tif ($line =~ \/\\\"workflowAutostartEnabled\\\": (.*),\/) {\n\t\t\t\t\t\t\t\tmy $as = $1;\n\t\t\t\t\t\t\t\tif ($as eq \"true\") {\n\t\t\t\t\t\t\t\t\t$autostart = \"Y\";\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ($line =~ \/\\\"workflowEnabled\\\": (.*),\/) {\n\t\t\t\t\t\t\t\tmy $en = $1;\n\t\t\t\t\t\t\t\tif ($en eq \"true\") {\n\t\t\t\t\t\t\t\t\t$enabled = \"Y\";\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ($line =~ \/\\\"workflowStarttime\\\": \\\"(.*)\\\"\/) {\n\t\t\t\t\t\t\t\t$startTime = $1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tclose(NSRW);\n\t\t\t\t\t}\n\t\t\t\t\tmy $wfd = \"Enabled [$enabled] Auto [$autostart] Time [$startTime]\";\n\t\t\t\t\tmy $state = \"\";\n\t\t\t\t\tif (open(NSRM,\"nsrpolicy monitor -p \\'$policy\\' -w \\'$workflow\\' --non-tabular 2>&amp;1 |\")) {\n\t\t\t\t\t\twhile (&lt;NSRM>) {\n\t\t\t\t\t\t\tmy $line = $_;\n\t\t\t\t\t\t\tchomp $line;\n\t\t\t\t\t\t\tif ($line =~ \/^\\s+job state:(.*)\/) {\n\t\t\t\t\t\t\t\t$state = $1;\n\t\t\t\t\t\t\t\tlast;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tclose(NSRM);\n\t\t\t\t\t\tif ($state eq \"COMPLETED\" || $state eq \"\") {\n\t\t\t\t\t\t\t$state = \"\";\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t$state=\"(\" . ucfirst(lc($state)) . \")\";\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tprint \"  -> $workflow $state\\n     $wfd\\n\";\n\t\t\t\t\tif (open(NSRA,\"nsrpolicy action list -p \\'$policy\\' -w \\'$workflow\\' |\")) {\n\t\t\t\t\t\twhile (&lt;NSRA>) {\n\t\t\t\t\t\t\tmy $line = $_;\n\t\t\t\t\t\t\tchomp $line;\n\t\t\t\t\t\t\tprint \"     - $line\\n\";\n\t\t\t\t\t\t}\n\t\t\t\t\t\tclose(NSRA);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<p>There&#8217;s a lot more you could do with this of course, but hopefully what you see from the above is how trivial it is to leverage the <em>nsrpolicy<\/em> to grab out useful details from the command line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The nsrpolicy tool is remarkably powerful, allowing you to create edit and view policy details rapidly from the command line&hellip;<\/p>\n","protected":false},"author":1,"featured_media":10670,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,16],"tags":[219,1201,1218],"class_list":["post-10668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-networker","tag-cli","tag-command-line","tag-nsrpolicy"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/nsrd.info\/blog\/wp-content\/uploads\/2021\/10\/bigStock-Programming.jpg","jetpack_shortlink":"https:\/\/wp.me\/pKpIN-2M4","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/10668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/comments?post=10668"}],"version-history":[{"count":2,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/10668\/revisions"}],"predecessor-version":[{"id":10673,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/posts\/10668\/revisions\/10673"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/media\/10670"}],"wp:attachment":[{"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/media?parent=10668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/categories?post=10668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nsrd.info\/blog\/wp-json\/wp\/v2\/tags?post=10668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}